
Last chance! 50% off unlimited learning
Sale ends in
ggsave()
is a convenient function for saving a plot. It defaults to
saving 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.
ggsave(
filename,
plot = last_plot(),
device = NULL,
path = NULL,
scale = 1,
width = NA,
height = NA,
units = c("in", "cm", "mm", "px"),
dpi = 300,
limitsize = TRUE,
bg = NULL,
create.dir = FALSE,
...
)
File name to create on disk.
Plot to save, defaults to last plot displayed.
Device to use. Can either be a device function
(e.g. png), or one of "eps", "ps", "tex" (pictex),
"pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only). If
NULL
(default), the device is guessed based on the filename
extension.
Path of the directory to save plot to: path
and filename
are combined to create the fully qualified file name. Defaults to the
working directory.
Multiplicative scaling factor.
Plot size in units expressed by the units
argument.
If not supplied, uses the size of the current graphics device.
One of the following units in which the width
and height
arguments are expressed: "in"
, "cm"
, "mm"
or "px"
.
Plot resolution. Also accepts a string input: "retina" (320), "print" (300), or "screen" (72). Applies only to raster output types.
When TRUE
(the default), ggsave()
will not
save images larger than 50x50 inches, to prevent the common error of
specifying dimensions in pixels.
Background colour. If NULL
, uses the plot.background
fill value
from the plot theme.
Whether to create new directories if a non-existing
directory is specified in the filename
or path
(TRUE
) or return an
error (FALSE
, default). If FALSE
and run in an interactive session,
a prompt will appear asking to create a new directory when necessary.
Other arguments passed on to the graphics device function,
as specified by device
.
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 technique is illustrated in the
examples section.
Note: Filenames with page numbers can be generated by including a C
integer format expression, such as %03d
(as in the default file name
for most R graphics devices, see e.g. png()
).
Thus, filename = "figure%03d.png"
will produce successive filenames
figure001.png
, figure002.png
, figure003.png
, etc. To write a filename
containing the %
sign, use %%
. For example, filename = "figure-100%%.png"
will produce the filename figure-100%.png
.
The saving section of the online ggplot2 book.
if (FALSE) {
ggplot(mtcars, aes(mpg, wt)) +
geom_point()
# here, the device is inferred from the filename extension
ggsave("mtcars.pdf")
ggsave("mtcars.png")
# setting dimensions of the plot
ggsave("mtcars.pdf", width = 4, height = 4)
ggsave("mtcars.pdf", width = 20, height = 20, units = "cm")
# passing device-specific arguments to '...'
ggsave("mtcars.pdf", colormodel = "cmyk")
# delete files with base::unlink()
unlink("mtcars.pdf")
unlink("mtcars.png")
# specify device when saving to a file with unknown extension
# (for example a server supplied temporary file)
file <- tempfile()
ggsave(file, device = "pdf")
unlink(file)
# save plot to file without using ggsave
p <-
ggplot(mtcars, aes(mpg, wt)) +
geom_point()
png("mtcars.png")
print(p)
dev.off()
}
Run the code above in your browser using DataLab