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.
using
ggplot2
I usually use the following 2 methods to create a common legend:Method 1 : When scales are similar
By using
facet_grid
or just thecolor
parameter in combination withreshape2
package, you can easily combine multiple plots with same legend. But this is ideal in case the values in your variables have a similar magnitude order.Using
color
&reshape2
:Using
color
,facet_grid
&reshape2
:Method 2: When scales differ wildly
As you can see,the final plot is great! All you need is to create a plot having your legend & pass it as an input parameter to the custom function created in the wiki here.