Beginner's guide to R: Painless data visualization

Learn how to paint a picture with data with R, using just a couple lines of code

Page 6 of 6

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.

TEXT
Exporting your graphics from RStudio.

To save a plot with R commands and not point-and-click, first create a container for your image using a function such as jpeg(), png(), svg(), or 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:

barplot(BOD$demand, col=rainbow(6))

Then issue the command:

dev.off()

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:

Part 1: Introduction to R

Part 2: Getting your data into R

Part 3: Easy ways to do basic data analysis with R

Part 4: Painless data visualization using R

Part 5: Syntax quirks you'll want to know about R

Part 6: Useful resources for R

This story, "Beginner's guide to R: Painless data visualization" was originally published by Computerworld.

| 1 2 3 4 5 6 Page 6
From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies