rasterpdf
is an R package to plot raster graphics in PDF files. This can be useful when one needs multipage documents, but the plots contain so many individual elements that use of vector graphics (with grDevices::pdf()
) results in inconveniently large file sizes. Internally, rasterpdf
plots each individual page as a PNG, and then combines them in one PDF file.
rasterpdf
can be installed from CRAN with:
Or the development version from GitHub with:
rasterpdf
’s main function raster_pdf()
can be used as any graphics device in R:
library(rasterpdf)
raster_pdf("Rplots.pdf")
plot(iris)
image(volcano)
plot(EuStockMarkets)
dev.off()
Or similarly with ggplot2
:
library(ggplot2)
library(rasterpdf)
raster_pdf("diamonds.pdf")
ggplot(diamonds, aes(carat, price)) + geom_point()
ggplot(diamonds, aes(carat, price, colour = color)) + geom_point()
ggplot(diamonds, aes(carat, price, colour = clarity)) + geom_point()
dev.off()
The output size and resolution can be set with parameters width
, height
, units
, and res
. The default is 7 x 7 inches and 72 ppi. Any other parameters (...
) are passed through to the PNG device function. By default this is grDevices::png()
, but another function can also be specified, such ragg::agg_png()
. The PDF file is by default generated with grDevices::cairo_pdf()
if cairo is available, and grDevices::pdf()
otherwise. Here again it is also possible to specify another function.