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:
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.
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
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.
There are also R functions that automatically generate a vector of n colors using a specific color palette such as
So, if you want five colors from the rainbow palette, use:
For many more details, check the help command on a palette such as:
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
mycolors <- heat.colors(3)
Now instead of using the
geom_bar() function without any arguments, add
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:
You can use a single color if you want all the items to be one color (but not monochrome):