Beginner's guide to R: Painless data visualization

Part 4 of our hands-on guide covers simple graphics, bar graphs and more complex charts.

If you had scores in a data frame called results with one column of student names called students and another column of scores called testscores, you could use the ggplot2 package's ggplot() function as well:

ggplot(results, aes(x=students, y=testscores)) + geom_bar(fill=testcolors, stat = "identity")

Coloring bars by factor
Coloring bars by factor.

Why stat = "identity"? That's needed here to show that the y axis represents a numerical value as opposed to an item count.

ggplot2's qplot() also has easy ways to color bars by a factor, such as number of cylinders, and then automatically generate a legend. Here's an example of graph counting the number of 4-, 6- and 8-cylinder cars in the mtcars data set:

qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(cyl))

But, as I said, we're getting somewhat beyond a beginner's overview of R when coloring by factor. For a few more examples and details for many of the themes covered here, you might want to see the online tutorial Producing Simple Graphs with R. For more on graphing with color, check out a source such as the R Graphics Cookbook. The ggplot2 documentation also has a lot of examples, such as this page for bar geometry.

If you're interested in more ideas for color palettes, check out the RColorBrewer and virdis packages.

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.

Rstudio graphics export
Exporting your graphics from RStudio.

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

Next: Syntax quirks you'll want to know.

This article, Beginner's guide to R: Easy data visualizations, was originally published at Computerworld.com.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

More about SimpleTest

Show Comments
[]