1

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))

enter image description here

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))

enter image description here

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.

arrrrRgh
  • 197
  • 3
  • 15
  • 2
    Here is hadley's explanation using `grid.arrange`: https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs – Rentrop Feb 23 '15 at 07:40
  • 1
    Why are you not facetting? http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/ – CMichael Feb 23 '15 at 09:31
  • you could add to p1 <- p1 + theme(legend.justification = 'left', legend.position=c(0,0.75)), while you add to the others + theme(legend.position = "none"). This will give you one legend in the top left of first plot. You may need to fiddle around a bit with the position values however. – Ruthger Righart Feb 23 '15 at 09:45
  • Yes, good call on the facetting @CMichael. I'm not facetting here as there are unique additional plot elements I'm hoping to add (e.g., significance stars for some plots and not others). Is there a way folks know to include some additional annotate( ) content for some but not all plots? – arrrrRgh Feb 23 '15 at 16:12
  • http://stackoverflow.com/questions/11889625/annotating-text-on-individual-facet-in-ggplot2 – CMichael Feb 23 '15 at 16:37
  • And concerning the original question: https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs – CMichael Feb 24 '15 at 09:57

1 Answers1

0

The following will show a single legend in the left top using theme(legend.justification = 'left', legend.position=c(0,0.75))

library(grid)

Data Example

x<-c(1,1,2,2)
y<-c(107, 110, 110, 118)
data<-data.frame(x,y)
data$group<-as.factor(c(1,2,1,2))

Ggplot

p1 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.justification = 'left', legend.position=c(0,0.75))
p2 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p3 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p4 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p5 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p6 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")

pushViewport(viewport(layout = grid.layout(3, 2)))  
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 = 2, layout.pos.col = 1))     
print(p4,vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
print(p5,vp = viewport(layout.pos.row = 3, layout.pos.col = 1))     
print(p6,vp = viewport(layout.pos.row = 3, layout.pos.col = 2))
Ruthger Righart
  • 4,799
  • 2
  • 28
  • 33