Sharing a Legend between two combined ggplots

筅森魡賤 提交于 2019-12-08 08:58:27

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 the color parameter in combination with reshape2 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:

library('reshape2')
data_melt<-melt(data=testdata,value.name='Value',id.vars='var1')

ggplot(data_melt)+
  geom_line(aes(x=var1,y=Value,color=variable))

Using color, facet_grid & reshape2:

library('reshape2')
data_melt<-melt(data=testdata,value.name='Value',id.vars='var1')

ggplot(data_melt)+
 geom_line(aes(x=var1,y=Value,color=variable))+
 facet_grid(~variable)

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.

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

library('reshape2')
data_melt<-melt(data=testdata,value.name='Value',id.vars='var1')

p1=ggplot(data = testdata)+
  geom_line(aes(x = var1, y = var2,color='blue'))

p2=ggplot(data = testdata) + 
  geom_line(aes(x = var1, y = var3,color='red'))

p3=ggplot(data_melt)+
   geom_line(aes(x=var1,y=Value,color=variable))

grid.arrange(p1,p2,nrow=2,main='Line Plots')

g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}

legend <- g_legend(p3)
lwidth <- sum(legend$width)

## using grid.arrange for convenience
## could also manually push viewports
grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
                         p2 + theme(legend.position="none"),
                         main ="Variable Name",
                         left = "Value"),
             legend, 
             widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!