capturePlot | R Documentation |
Easily generates a .png file (or other graphics file) from a plotting expression.
capturePlot(
expr,
filename = tempfile(fileext = ".png"),
device = defaultPngDevice(),
width = 400,
height = 400,
res = 72,
...
)
expr |
A plotting expression that generates a plot (or yields an object
that generates a plot when printed, like a ggplot2). We evaluate this
expression after activating the graphics device ( |
filename |
The output filename. By default, a temp file with |
device |
A graphics device function; by default, this will be either
|
width , height , res , ... |
Additional arguments to the |
plotTag()
saves plots as a self-contained <img>
tag.
# Default settings
res <- capturePlot(plot(cars))
# View result
browseURL(res)
# Clean up
unlink(res)
# Custom width/height
pngpath <- tempfile(fileext = ".png")
capturePlot(plot(pressure), pngpath, width = 800, height = 375)
browseURL(pngpath)
unlink(pngpath)
# Use a custom graphics device (e.g., SVG)
if (capabilities("cairo")) {
svgpath <- capturePlot(
plot(pressure),
tempfile(fileext = ".svg"),
grDevices::svg,
width = 8, height = 3.75
)
browseURL(svgpath)
unlink(svgpath)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.