Skip to contents
library(wgpugd)
library(ragg)

agg <- function(...) {
  agg_png(width = 7, height = 7, units = "in", ...)
}

Line

do_line <- function(dev) {
  filename <- knitr::fig_path(paste0(deparse(substitute(dev)), ".png"))
  dev(filename = filename)

  a <- pi * 1:3 / 7
  x <- sin(a)
  y <- cos(a)
  col <- c("red", "blue", "green")
  le <- c("round", "butt", "square")
  
  invisible(lapply(1:3, \(i) {
    grid::grid.lines(
      x = 0.5 + c(-0.35 * x[i], 0.4 * x[i]),
      y = 0.5 + c(-0.35 * y[i], 0.4 * y[i]),
      gp = grid::gpar(
        col  = col[i],
        alpha = 0.5,
        lwd = 50 + 24 * i,
        lineend = le[i],
        linemitre = 10.
      )
    )
  }))

  dev.off()
  
  knitr::include_graphics(filename)
}

do_line(agg)
do_line(wgpugd)

Rect

do_rect <- function(dev) {
  filename <- knitr::fig_path(paste0(deparse(substitute(dev)), ".png"))
  dev(filename = filename)

  grid::grid.rect(
    x = c(0.8, 0.3),
    y = c(0.8, 0.3),
    width = c(0.8, 0.7),
    height = c(0.8, 0.45),
    gp = grid::gpar(
      fill  = c("green", "blue"),
      col   = c("yellow"),
      lwd   = 40,
      alpha = 0.5
    )
  )

  dev.off()
  
  knitr::include_graphics(filename)
}

do_rect(agg)
do_rect(wgpugd)

Polygon

From ?grid.polygon

do_polygon <- function(dev) {
  filename <- knitr::fig_path(paste0(deparse(substitute(dev)), ".png"))
  dev(filename = filename)

  grid::grid.polygon(x = outer(c(0, .5, 1, .5), 5:1/5),
                     y = outer(c(.5, 1, .5, 0), 5:1/5),
                     id.lengths = rep(4, 5),
                     gp = grid::gpar(fill = 1:5, alpha = 0.5))

  dev.off()
  
  knitr::include_graphics(filename)
}

do_polygon(agg)
do_polygon(wgpugd)

Circle

do_circle <- function(dev) {
  filename <- knitr::fig_path(paste0(deparse(substitute(dev)), ".png"))
  dev(filename = filename)

  grid::grid.circle(
    x = c(0.8, 0.3),
    y = c(0.8, 0.3),
    r = c(0.5, 0.7),
    gp = grid::gpar(
      fill  = c("green", "blue"),
      col   = c("yellow"),
      lwd   = 40,
      alpha = 0.5
    )
  )
  dev.off()
  
  knitr::include_graphics(filename)
}

do_circle(agg)
do_circle(wgpugd)

Text

do_text <- function(dev) {
  filename <- knitr::fig_path(paste0(deparse(substitute(dev)), ".png"))
  dev(filename = filename)

  x <- y <- grid::unit(0.5, "npc")
  grid::grid.points(x = x, y = y)
  lapply(72 * 7 / 2 + (-2):2 * 20, function(x) {
    grid::grid.lines(x = grid::unit(c(0, 1), "npc"), y = grid::unit(c(x, x), "points"))
  })
  grid::grid.text(label = "gla\nd", hjust = 1, vjust = 0, gp = grid::gpar(cex = 5, col = "red"))
  grid::grid.text(label = "ve\nrrr\nrry", hjust = 0, vjust = 1, gp = grid::gpar(cex = 5, col = "blue"))
  grid::grid.text(label = "rot", x = 0.4, y = 0.4, hjust = 0.5, vjust = 0.5, rot = 30, gp = grid::gpar(cex = 5, col = "green", alpha = 0.4))
  dev.off()
  
  knitr::include_graphics(filename)
}

do_text(agg)
do_text(wgpugd)

do_text2 <- function(dev) {
  filename <- knitr::fig_path(paste0(deparse(substitute(dev)), ".png"))
  dev(filename = filename)

  grid::grid.text(
    x = grid::unit(0.5, "npc"),
    y = grid::unit(0.5, "npc"),
    label = "d",
    hjust = 0.5,
    vjust = 0,
    gp = grid::gpar(col = "brown", cex = 24, fontfamily = "Iosevka SS08")
  )
  
  f <- function(x, ...) {
    grid::grid.lines(
      x = grid::unit(c(0, 1), "npc"),
      y = grid::unit(rep(12, 2) * x, "points") + grid::unit(c(0.5, 0.5), "npc"),
      gp = grid::gpar(...)
    )
    invisible(NULL)
  }
  
  lapply(1:20, f, lty = 5, col = "purple", alpha = 0.7)
  lapply(0:2 * 10, f, col = "black")

  dev.off()
  
  knitr::include_graphics(filename)
}

do_text2(agg)
do_text2(wgpugd)

Order of elements

do_order <- function(dev) {
  filename <- knitr::fig_path(paste0(deparse(substitute(dev)), ".png"))
  dev(filename = filename)

  grid::grid.circle(x = 0.3, r = 0.3, gp = grid::gpar(col = "transparent", fill = "#FF000080"))
  grid::grid.circle(x = 0.7, r = 0.3, gp = grid::gpar(col = "transparent", fill = "#00FF0080"))
  grid::grid.lines(y = 0.5,  gp = grid::gpar(col = "#FFFF0080", lwd = 60))
  grid::grid.circle(y = 0.7, r = 0.3, gp = grid::gpar(col = "transparent", fill = "#0000FF80"))
  grid::grid.lines(x = 0.8,  gp = grid::gpar(col = "#FF00FF80", lwd = 60))
  
  dev.off()
  
  knitr::include_graphics(filename)
}

do_order(agg)
do_order(wgpugd)

do_order2 <- function(dev) {
  filename <- knitr::fig_path(paste0(deparse(substitute(dev)), ".png"))
  dev(filename = filename)

  col <- scales::alpha("red", 0.7)
  grid::grid.circle(x = 0.2, r = 0.2, gp = grid::gpar(col = col, fill = "blue", lwd = 40))
  grid::grid.lines(y = 0.55, gp = grid::gpar(col = "#00000088", lwd = 50))
  grid::grid.rect(width = 0.4, height = 0.4, gp = grid::gpar(fill = "pink"))
  grid::grid.lines(y = 0.45, gp = grid::gpar(col = "#00000088", lwd = 60))
  grid::grid.circle(x = 0.8, r = 0.2, gp = grid::gpar(fill = "lightgreen", lwd = 40))
  grid::grid.lines(y = 0.35, gp = grid::gpar(col = "#00000088", lwd = 60))

  dev.off()
  
  knitr::include_graphics(filename)
}

do_order2(agg)
do_order2(wgpugd)