ggplot2: Three Variable Time Series Panel Chart
May 18, 2009
The three variable time series panel chart published in Charts & Graphs blog conveniently combines different chart formats and colour formatting.

Next, a similar chart will be constructed in ggplot2.
First, import the data file and convert it to a long format. After that representations of different subsets of data will be added layer-by-layer.
> library(ggplot2) > df <- read.csv("Consol_GISS_SATO_Nino34A_RSS.csv") > df[, "GISS_A_C"] <- NULL > dfm <- melt(df, id.vars = c(1:3), na.rm = TRUE) |
A few things to note here:
- scale_colour_identity() maps the colours to the colour variables, i.e. variable with the value “red” will be plotted in red.
- as.table=FALSE argument in facet_grid() is used to reverse the order of facets.
> f <- ggplot(dfm, aes(yr_frac, value, ymin = 0, ymax = value, colour = "grey30")) + scale_colour_identity() + xlim(1980, 2010) + facet_grid(variable ~ ., scales = "free", as.table = FALSE) + theme_bw() + opts(panel.margin = unit(0, "lines"), axis.title.x = theme_blank(), axis.title.y = theme_blank()) |
> f1 <- f + geom_step(subset = .(variable == "RSS")) |

> f2 <- f1 + geom_linerange(subset = .(variable == "SATO")) |

> NINO34 <- subset(dfm, dfm$variable == "NINO34A") > f3 <- f2 + geom_linerange(data = NINO34, aes(colour = ifelse(value < 0, "blue", "red"))) + opts(title = "RSS and NINO3.4 Temperature Anomalies \nand SATO Index Trends Since 1980") |
> f3 |

19 Comments
leave one →
Trackbacks
- Area Plots with Intensity Coloring ~ el nino SST anomalies w/ ggplot2 « mind of a Markov chain
- ggplot2: Three Variable Time Series Panel Chart (via Learning R) | Human Mathematics
- A ggplot trick to plot different plot types in facets « Stat Bandit
- A ggplot trick to plot different plot types in facets | StatBandit
Shouldn’t the year labels be centered between the tickmarks that indicate the beginning and the end of each year?
I don’t quite understand what you mean – currently the tick marks and labels are every five years.
If this was monthly data, and the tick marks were indicating the beginning and the end of each year, then, yes, the labels should be centered between the tick marks.
have you had any luck overlaying charts? it isn’t immediately obvious to me how to do it.
i have plenty of circumstances where i want to plot a bunch of points and then overlay (or underlay) some kind of boxplot or curve on them, but i’m having trouble finding out how to do it from the docs for ggplot.
ggplot2 is designed to work in a layered fashion. So to accomplish the task you are describing you first plot the points, and then add another layer with for example boxplot.
Consider the following example:
dx <- data.frame(x=1:10, y=sample(10))
ggplot(dx, aes(x, y)) + geom_point(size=4) + geom_line()
The resulting plot consists of three layers: the raw data layer, points layer and lines layer. You can add as many layers as you wish.
how about if you wanted to overlay a different dataset on top of what you have above?
In overlaying a different dataset, it needs to be mapped to the layer (this overrides the original mapping), and if the data structure is different than that in the original dataset, new mappings need to be created.
See example below where three different datasets are overlaid on the same plot: each layer is mapped to a different dataset.
dx <- data.frame(x=1:10, y=sample(10))
dy <- data.frame(a=1:10, b=sample(11:20))
dz <- data.frame(f=1:10, g=-sample(10))
ggplot(dx, aes(x, y)) +
geom_point(size = 4) +
geom_point(data = dy, aes(a, b), colour= "red") +
geom_line(data = dz, aes(f, g), colour = "steelblue", linetype = 2, size = 2)
aha, i think i see how it works. if you change the mapping, do all subsequent layers refer to that mapping unless otherwise specified?
thank you!
Yes, if no specific mapping exists on a layer then it refers back to the default mapping specified in the initial ggplot() call.
There’s a good explanation of mapping in Chapter 4 of the forthcoming ggplot2 book.
Hi,
How would you overlay for example RSS and SATO with different colors and then have a legend????
Any sense of how one might add a shaded interval to the above graph? Say you wanted to draw particular attention to 1990-2000. Could you use geom_rect() to do this, somehow? Furthermore, could how might you achieve this on just one of the three graphs, or on all of them?
Yes,
geom_rect()
is perfectly suitable for highlighting a particular period. If you want to do this on three graphs, then the best option would be to create a new data frame with the necessary data, and then base thegeom_rect()
layer on this.Is it possible to add y axis labels for each plot? (labeling the units for example)
There is no straightforward way to accomplish this, as far as I can see. I would like to be proved wrong, though.
Hi, thanks for this example. It is really useful for the kind of I am currently trying to produce.
My question is the same: any way to insert independent y-axis labels? It’s been a long since your last response, so I thought that maybe there’s a new feature on ggplot2 that allows that.
I’m having trouble reproducing this trick. Before I make a reproducible example and ask the world at large, I see that this example uses the old “opts” format. Is there something in this code that might break now? I can’t see why but darned if I know what’s wrong, something in the subset function (currently it makes extra blank panels for all the variables that aren’t being called in the subset) and I don’t know how to leave them out.
pbase <- ggplot(dat, aes(x = year, y = value, ymin = 0, ymax = value)) + theme_bw() +
facet_grid(variable ~ ., scales="free_y")
p1 <- pbase + geom_line(subset = .(variable == "totalpop"))