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 4 of 6

Additional graphics options

There are many more graphics types in R than these few I've mentioned. Boxplots, a statistical staple showing minimum and maximum, first and third quartiles and median, have their own function called, intuitively, boxplot(). If you want to see a boxplot of the MPG column in the mtcars data frame it's simple:

boxplot(mtcars$mpg)

To see side-by-side boxplots in a single plot, such as the x, y, and z measurements of all the diamonds in the diamonds sample data set included in ggplot2:

boxplot(diamonds$x, diamonds$y, diamonds$z)

Creating a heat map in R is more complex but not ridiculously so. There's an easy-to-follow tutorial on Flowing Data.

You can do graphical correlation matrices with the corrplot add-on package and generate numerous probability distributions. See some of the links here or in the resources section to find out more.

Using color

Looking at nothing but black and white graphics can get tiresome after a while. Of course, there are numerous ways of using color in R.

Colors in R have both names and numbers as well as the usual RGB hex code, HSV (hue, saturation and value) specs and others. And when I say "names," I don't mean just the usual "red," "green," "blue," "black" and "white." R has 657 named colors. The colors() or colours() function -- R does not discriminate against either American or British English -- gives you a list of all of them. If you want to see what they look like, not just their text names, you can get a full, multipage PDF chart with color numbers, colors names and swatches, sorted in various ways. Or you can find just the names and color swatches for each.

TITLE
Using three colors in the R rainbow palette.

There are also R functions that automatically generate a vector of n colors using a specific color palette such as rainbow or heat:

rainbow(n)

heat.colors(n)

terrain.colors(n)

topo.colors(n)

cm.colors(n)

So, if you want five colors from the rainbow palette, use:

rainbow(5)

For many more details, check the help command on a palette such as:

?rainbow

Now that you have a list of colors, how do you get them in your graphic? Here's one way. Say you're drawing a three-bar barchart using ggplot() and want to use thre colors from the rainbow palette. You can create a three-color vector like:

mycolors <- rainbow(3)

Or for the heat.colors pallette:

mycolors <- heat.colors(3)

Now instead of using the geom_bar() function without any arguments, add fill=mycolors to geombar() like this:

ggplot(mtcars, aes(x=factor(cyl))) + geom_bar(fill=mycolors)

You don't need to put your list of colors in a separate variable, by the way; you can merge it all in a single line of code:

ggplot(mtcars, aes(x=factor(cyl))) + geom_bar(fill=rainbow(3))

But it may be easier to separate the colors out if you want to create your own list of colors instead of using one of the defaults.

The basic R plotting functions can also accept a vector of colors:

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

You can use a single color if you want all the items to be one color (but not monochrome):

barplot(BOD$demand, col="royalblue3")

| 1 2 3 4 5 6 Page 4
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