Instead of having to repeat "height= 4, width= 4, dpi= 72" with every call . . .
I once tried library(defaults) for a problem like this, but it did not behave well. I asked a question about it on R-help, but it seemed like no one uses it.
It looks like par("din") is coming from the X11 device that I have running, but that's not going to be the device that ggsave() uses when I call it, I don't think, because I'm either saving to a .png or a .pdf -- PDFs are no good for my scatter and tile plots. Granted, the PDFs scale much better when I bring them into my LaTeX document, but it seems like this is a desirable level of control in general.
Thanks for your ideas.
2.1. The default size of the saved image is equal to the size of Plots pane (the “graphics device”) in RStudio, which can be found with dev. size() . Notice that the result of dev. size() and the message we receive when saving the plot with ggsave() give the same dimensions.
ggsave. ggsave works by default on the most recent graph that you've plotted. The only arugment it needs is a file name to save it as. By default, it will save to the directory that you're working out of.
You can either print directly a ggplot into PNG/PDF files or use the convenient function ggsave() for saving a ggplot. The default of ggsave() is to export the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.
In most cases ggsave() is the simplest way to save your plot, but sometimes you may wish to save the plot by writing directly to a graphics device. To do this, you can open a regular R graphics device such as png() or pdf() , print the plot, and then close the device using dev. off() .
This may not be the best way, but you can write a wrapper function with different height and width defaults. For example
my.ggsave <- function(filename = default_name(plot), height= 4, width= 4, dpi= 72, ...) {
ggsave(filename=filename, height=height, width=width, dpi=dpi, ...)
}
Now we can test to see if my.ggsave does what we want it to:
ggplot(data.frame(x=1:10), aes(x=x, y=x)) + geom_point()
ggsave("normal_ggsave.png")
and make sure we can pass additional arguments to ggsave
if we need to:
my.ggsave("four_by_four_600.png", dpi=600)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With