write figure to pdf, tif, png, jpg, svg, or emf, according to file suffix.

write_fig(
  p,
  file = "Rplot.pdf",
  width = 10,
  height = 5,
  devices = NULL,
  res = 300,
  show = TRUE,
  use.cairo_pdf = TRUE,
  use.file_show = FALSE
)

Arguments

p

could be one of grid, ggplot or plot expression

file

file path of output figure

width

the width of the device in inches.

height

the height of the device in inches.

devices

can be c("pdf", "tif", "tiff", "png", "jpg", "svg", "emf"). If not specified, devices will be determined according to the postpix of file. The default type is pdf.

res

The nominal resolution in which will be recorded in the bitmap file, if a positive integer. Also used for units other than the default. If not specified, taken as 72 to set the size of text and line widths.

show

Boolean. Whether show file after finished writing?

use.cairo_pdf

This parameter is for pdf type. whether to use grDevices::cairo_pdf? cairo_pdf supports self defined font, but can not create multiple page pdf.

use.file_show

boolean. If true, file.show will be used mandatorily.

Examples

if (FALSE) { # \dontrun{
library(ggplot2)
p <- ggplot(mpg, aes(class, hwy))
p1 <- p + geom_boxplot2()

## ggplot version
write_fig(p1, "Fig. 1. ggplot.pdf", show = TRUE)
# pdf_view("Fig. 1. ggplot.pdf")
write_fig(p1, "Fig. 1. ggplot", show = TRUE)
write_fig(p1, "Fig. 1. ggplot.pdf", show = TRUE, 
    devices = c("jpg", "png", "svg", "pdf", "tif", "emf"))

## lattice
x <- seq(pi/4, 5 * pi, length.out = 100)
y <- seq(pi/4, 5 * pi, length.out = 100)
r <- as.vector(sqrt(outer(x^2, y^2, "+")))
grid <- expand.grid(x=x, y=y)
grid$z <- cos(r^2) * exp(-r/(pi^3))
p <- levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="",
          ylab="", main="Weird Function", sub="with log scales",
          colorkey = FALSE, region = TRUE)

write_fig(p, "fig_lattice", show = TRUE)

## grid expression
g <- grid::circleGrob()
write_fig(g, "fig_grid", show = TRUE)

## R expression
write_fig({
    rx <- range(x <- 10*1:nrow(volcano))
    ry <- range(y <- 10*1:ncol(volcano))
    ry <- ry + c(-1, 1) * (diff(rx) - diff(ry))/2
    tcol <- terrain.colors(12)
    # par(opar); 
    # opar <- par(pty = "s", bg = "lightcyan")
    plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "")
    u <- par("usr")
    rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red")
    contour(x, y, volcano, col = tcol[2], lty = "solid", add = TRUE,
            vfont = c("sans serif", "plain"))
    title("A Topographic Map of Maunga Whau", font = 4)
    abline(h = 200*0:4, v = 200*0:4, col = "lightgray", lty = 2, lwd = 0.1)
}, "fig_expr.pdf")

# quote expression also works
expr = quote({
    rx <- range(x <- 10*1:nrow(volcano))
    ry <- range(y <- 10*1:ncol(volcano))
    ry <- ry + c(-1, 1) * (diff(rx) - diff(ry))/2
    tcol <- terrain.colors(12)
    # par(opar); 
    # opar <- par(pty = "s", bg = "lightcyan")
    plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "")
    u <- par("usr")
    rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red")
    contour(x, y, volcano, col = tcol[2], lty = "solid", add = TRUE,
            vfont = c("sans serif", "plain"))
    title("A Topographic Map of Maunga Whau", font = 4)
    abline(h = 200*0:4, v = 200*0:4, col = "lightgray", lty = 2, lwd = 0.1)
})
write_fig(expr, "fig_expr2.pdf")
} # }