在两个合并的ggplots之间共享一个图例 [英] Sharing a Legend between two combined ggplots

查看:718
本文介绍了在两个合并的ggplots之间共享一个图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用ggplot2来呈现两个时间序列,两个都使用两个不同的尺度,使用两个ggplots。我已经使用 grid.arrange 组合了两个单独的ggplots,一个在另一个之上。为了有助于可视化,我想让每一行都有不同的颜色,并将这个图例放在组合图的下方。

由于这可能是相关的,我目前正在创建一个R markdown文档的闪亮部分。因此renderPlot封装在 grid.arrange



下面与我目前的代码类似。 / p>

  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(color =blue)+ xlab(NULL),
ggplot(data = testdata,aes(x = var1,y = var3))+ geom_line(color =红色))})

有没有人对如何创建共享图例有任何建议?非常感谢您的帮助。

使用 ggplot2 我通常会使用

解决方案以下两种方法来创建一个常见的图例:

方法1:当比例相似时



通过使用 facet_grid 或仅将 color 参数与 reshape2 包,您可以轻松地将多个图与相同的图例组合在一起。但是,如果变量中的值具有相似的幅度顺序,这是理想的。

使用 color $ b &安培; 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))



使用 color facet_grid & reshape2

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

ggplot(data_melt)+
geom_line(aes(x =变量1,y =值,颜色=变量))+
facet_grid(变量)



方法2:当量表差异很大时





正如你所看到的,最后的情节非常棒!
所有你需要的是创建一个有你的传奇和绘图的情节。将它作为输入参数传递给 wiki这里

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

library('reshape2')
data_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)

##使用grid.arrange为了方便
##也可以手动推视口
grid.arrange(arrangeGrob(p1 + theme(legend.position =none),
p2 + theme(legend.position =none),
main =变量名称,
left =Value),
legend,
widths = unit .c(unit(1,npc) - lwidth,lwidth),nrow = 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.

解决方案

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)

这篇关于在两个合并的ggplots之间共享一个图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆