Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Beginner's guide to R: Painless data visualisation

Sharon Machlis | June 7, 2013
One of the most appealing things about R is its ability to create data visualizations with just a couple of lines of code.

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))

And 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. And, also in RStudio, to see previous plots from your session, click the back arrow.

 

Previous Page  1  2  3  4  5  6  7 

Sign up for CIO Asia eNewsletters.