View source: R/savePlotToFile.R
| savePlotToFile | R Documentation |
Helper function to save ggplot2 plots to files with consistent settings and error handling. Supports PNG, PDF, and SVG formats with configurable dimensions and resolution.
savePlotToFile(
plot,
file,
format = NULL,
width = 8L,
height = 6L,
dpi = 150L,
units = "in",
bg = "white"
)
plot |
A ggplot2 plot object to save. If NULL, returns FALSE. |
file |
character. The file path to save the plot to. |
format |
character. Output format: "png", "pdf", or "svg". Defaults to "png". If not specified, format is inferred from file extension. |
width |
numeric. Plot width in inches. Defaults to 8. |
height |
numeric. Plot height in inches. Defaults to 6. |
dpi |
numeric. Resolution in dots per inch for raster formats. Defaults to 150 for good quality web/screen use. Use 300 for print. |
units |
character. Units for width and height. Defaults to "in" (inches). |
bg |
character. Background color. Defaults to "white". |
Logical. TRUE if the file was saved successfully, FALSE otherwise.
ggsave for the underlying save function
## Not run:
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
savePlotToFile(p, "my_plot.png")
savePlotToFile(p, "my_plot.pdf", format = "pdf")
savePlotToFile(p, "high_res.png", dpi = 300)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.