I'm trying to create a multipanel plot with a single legend using ggplot. I've been able to create a multipanel plot by first creating my six individual plots using code like this:
p1 <- ggplot(data, aes(y = value, x = Time))+
geom_point(position="dodge")+geom_line(aes(group=group,linetype=group))
I used similar code for five additional plots. I used the pushViewport function in the 'grid' library to create a mutlipanel plot:
pushViewport(viewport(layout = grid.layout(2, 3)))
print(p1,vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(p2,vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
print(p3,vp = viewport(layout.pos.row = 1, layout.pos.col = 3))
print(p4,vp = viewport(layout.pos.row = 2, layout.pos.col = 1))
print(p5,vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
print(p6,vp = viewport(layout.pos.row = 2, layout.pos.col = 3))
What I'd like to do now but can't seem to figure out is add a single legend for all six plots. I've read some about adding a legend outside of the plotting boundary but can't seem to get this to work alongside the pushViewport method.