1

I'm currently trying to present two time series using ggplot2, both with very different scales, using two ggplots. I've combined the two separate ggplots, one on top of the other, using grid.arrange. In order to aid visualization, I'd like to make each line a different colour, and have this legend below the combined plot.

As this may be relevant, I'm currently working in the confines of creating a shiny section of an R markdown document. Hence the renderPlot wrapper around grid.arrange.

The following is similar to the code that I currently have.

testdata = data.frame(var1 = seq(0,10,by=1), var2 = runif(11), 
var3 = runif(11, min = 100, max = 500))

renderPlot({grid.arrange(
ggplot(data = testdata, aes(x = var1, y = var2))
+ geom_line(colour = "blue") + xlab(NULL),
ggplot(data = testdata, aes(x = var1, y = var3)) + geom_line(colour = "red"))})

Does anyone have any suggestions about how to create the shared legend? Thanks very much for your help.

Tom
  • 21
  • 2

0 Answers0