Smartening up plots with ggplot

R is a wonderfully powerful tool for the production of publication-quality figures. The power of R to make these plots comes at a cost: with absolutley everything customizable the learning curve is steep, and the defaults can be ugly. Lets take a look at a default plot.

x <- seq(0, 2 * pi, , 100)
y <- 1e+10/(1 - x^2) + 4e+11

plot(x, y, type = "b")

plot of chunk unnamed-chunk-1

Here, I’ve generated two vectors called x and y. x is a sequence of 100 points from 0 to 2*pi, and y is just some sample function that looks a little like a resonance. It’s not bad, but R can do better.

In order to tart this plot up, we’re going to load up three packages.

library(ggplot2)

ggplot2 is a graphics system for R that is based on the ‘grammar of graphics’. It’s a popular and powerful package, but we’re only going to scratch the surface here.

Lets load up the other two packages.

library(photonMonkey)
library(scales)

The photonMonkey package is my collection of utility functions for common tasks I’ve come across in my work. It’s avalible on github. scales is currently required to smarten up those axis labels.

So ggplot2‘s utility plot function for quick plots is qplot(), which in most ways works exactly like the default plot() function. The default looks like this:

qplot(x, y, geom = c("line", "point"))

plot of chunk unnamed-chunk-4

A key difference between qplot and plot is that qplot returns a ggplot object, which allows you to add layers. To smarten up this plot, I have added 5 layers, and this is the result:

qplot(x,y,geom=c("line","point"))+
  theme_bw()+
  scale_y_continuous(labels=label_scientific10)+
  scale_x_continuous(expand=c(0,0))+
  xlab(label_frequency(si_prefix="G"))+
  ylab("SCS (arb)")

plot of chunk unnamed-chunk-5

So, each of these layers helps pretty-fy the plot,

  • theme_bw() is a theme for the plot, which removes the default grey background and changes the colour scheme. There are many many themes (even, gasp, an excel theme).
  • scale_y_continuous(labels=label_scientific10) provides nice scientific style formatting to the large y value numbers.
  • scale_x_continuous(expand=c(0,0)) removed the padding on the x-axis.
  • xlab(label_frequency(si_prefix="G")) adds a nicely formatted axis label. photonMonkey includes loads of photonics/plasmonic themed labels like this.
  • ylab is just a custom label. I pretended this plot was a scattering-cross-section.

So, from a basic plot to a rather nicer looking one, thats some very-basics of using ggplot for plotting. The next post will deal with colourplots, whicih crop up loads in physics and, with the right functions, are produced very nicely in R.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s