At some point, you will have to save your plot as a picture file. There are two simple options:
ggsave()
. ggsave()
If after a few hours coding your plot with care and patience, you still have some energy for coding a bit more, then you may save your plot with the function ggsave()
. By default, the function saves the last plot that has been produced into a file which name must be specified. The simplest code is thus something like this:
ggsave("test.png")
## Saving 7 x 5 in image
Rstudio tells you that the image has been produced and indicates the dimensions (in inches). This image has been placed by default in your working directory.
If you want to have better control over the file properties, you may add a series of arguments to ggsave(). Some useful arguments are:
dpi=
which defines the image resolution in dots per inch, use 72 (OK for screens) or 300 (best for prints),width=
, which defines the width of the image,height=
, which defines the height of the image,units=
, which defines the unit to apply when defining the dimensions, use “cm”
for centimeters, “mm”
for millimeters or “in”
for inches.Finally the filename should also contain the extension that you wish (.pdf, .jpg, .jpeg, .png, .svg, .tiff, .eps, etc).
ggsave("test.jpg", dpi = 300, width = 20, height = 20, units = "cm")