Exporting your graphics
You can save your R graphics to a file for use outside the R environment. RStudio has an export option in the plots tab of the bottom right window.
If you are using "plain" R in Windows, you can also right-click the graphics window to save the file.
To save a plot with R commands and not point-and-click, first create a container for your image using a function such as
pdf(). Those functions need to have a file name as one argument and optionally width and height, such as:
jpeg("myplot.jpg", width=350, height=420)
Generate the plot with whatever graphics functions it requires, such as:
Then issue the command:
That will save your graphic to its container.
If you are using ggplot2, you can also use the function
ggsave(), which defaults to saving the last plot you created using ggplot2 at the size you displayed it. Based on the filename you pass to the
ggsave() function, it will save in the appropriate format -- myplot.jpg saves as a JPEG, myplot.png saves as a PNG and so on.
One final note: If you're working in RStudio and would like to see a larger version of your plot, click the Zoom button and a larger window with the current graphic will open. Also in RStudio, to see previous plots from your session, click the back arrow.
See the entire beginner's guide to R:
This story, "Beginner's guide to R: Painless data visualization" was originally published by Computerworld.