控制ggplot2中多层图的图例 [英] Control over legends of multiple layer plot in ggplot2

查看:145
本文介绍了控制ggplot2中多层图的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与 R:多层ggplot自定义图例,然后为多个图层ggplot2设置图例,即:我想要为多层图创建自定义图例。然而,有一个细微的差别:
在原始问题中,期望的效果是从两种不同的分组方法中分离出来: fill 颜色,这就是为什么可以使用两个不同的 scale_XXX 函数。在我的情况下,我创建了一个包含点(一层)和线(第二层)的图
。两种层的颜色都不同:

  x < -  seq(0,10,.1)
y< - sin(x)
lbl< - ifelse(y> 0,'positive','non-positive')
data.one < - data.frame(x = x,y =数据帧(x = c(0,10,0,10),y = c(-0.5,-0.5,0.5,0.5,-0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5 ),分类= c('低','低','高','高'))
plt< - ggplot(data.one)+ geom_point(aes(x,y,color = lbl) )+ scale_color_discrete(name ='one',guide ='legend')
plt < - plt + geom_line(data = data.two,aes(x,y,color = classification))+ scale_color_discrete(name = 'two',guide ='legend')
print(plt)

这是结果:



我想要将点和线的图例分开,以便图例如下所示:



我无法找到一种方法来采用引用问题的方法来处理我的情况。任何想法?

解决方案

以下是一个破解。它从临时地块中提取图例,然后使用 grid.arrange 来合并所有内容。

 <$ c (sapply(tmp $ grobs,function(x))); 
$ b< - 函数(a.gplot){
tmp< - ggplot_gtable(ggplot_build(a.gplot) x $ name)==guide-box)
legend <-tmp $ grobs [[leg]]
return(legend)}

n < - 4; (h = seq(15,375-360 / n,长度= n)%% 360,c = 100,1 = 65)

cols1< - cols [4: 3]
名称(cols1)< - c(positive,non-positive)
plt_1< - ggplot(data.one)+
geom_point(data = data。 aes(x,y,color = lbl))+
scale_color_manual(values = cols1)


cols2< - cols [1:2]
名称(cols2)<-c(high,low)
plt_2 < - ggplot(data.one)+
geom_line(data = data.two,aes(x,y,颜色=分类))+
scale_color_manual(values = cols2)


mylegend_1< -g_legend(plt_1)
mylegend_2< -g_legend(plt_2)

plt < - ggplot(data.one)+
geom_point(data = data.one,aes(x,y,color = lbl))+
geom_line(data = data.two ,aes(x,y,color = classification))+
scale_color_discrete(guide =none)

library(gridExtra)
grid.arrange(plt,
arrangeGrob(mylegend_1,mylegend_2,nrow = 6),
ncol = 2,widths = c(7,1))



你需要再多一点以获得您预期产出的理由。


My question is closely related to R: Custom Legend for Multiple Layer ggplot , and to Format legend for multiple layers ggplot2 namely: I want to create custom legends for multiple-layer plot. However, there is a subtle difference: In the original questions, the desired effect was to separate from two different groupping methods: fill and color and that's why it was possible to use two different scale_XXX functions. In my case I create a plot that contains points (one layer) and lines (second layer). Both layers are differentiated by color:

x <- seq(0, 10, .1)
y <- sin(x)
lbl <- ifelse(y > 0, 'positive', 'non-positive')
data.one <- data.frame(x=x, y=y, lbl=lbl)

data.two <- data.frame(x=c(0, 10, 0, 10), y=c(-0.5, -0.5, 0.5, 0.5), classification=c('low', 'low', 'high', 'high'))
plt <- ggplot(data.one) + geom_point(aes(x, y, color=lbl)) + scale_color_discrete(name='one', guide='legend')
plt <- plt + geom_line(data=data.two, aes(x, y, color=classification)) + scale_color_discrete(name='two', guide='legend')
print(plt)

Here is the result:

What I want is to separate the legends for points and lines, so that the legend looks like this:

I could not find a way to adopt the approach of the cited questions to my situation. Any ideas?

解决方案

The following is a hack. It extracts the legends from temporary plots and then combines everything using grid.arrange.

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

n <- 4; cols <- hcl(h=seq(15, 375-360/n, length=n)%%360, c=100, l=65)

cols1 <- cols[4:3]
names(cols1) <-  c("positive", "non-positive")
plt_1 <- ggplot(data.one) + 
  geom_point(data=data.one,aes(x, y, color=lbl)) +
  scale_color_manual(values=cols1)


cols2 <- cols[1:2]
names(cols2) <-  c("high", "low")
plt_2 <- ggplot(data.one) + 
  geom_line(data=data.two, aes(x, y, color=classification)) +
  scale_color_manual(values=cols2)


mylegend_1<-g_legend(plt_1)
mylegend_2<-g_legend(plt_2)

plt <- ggplot(data.one) + 
  geom_point(data=data.one,aes(x, y, color=lbl)) +
  geom_line(data=data.two, aes(x, y, color=classification)) +
  scale_color_discrete(guide="none")

library(gridExtra)
grid.arrange(plt,
             arrangeGrob(mylegend_1, mylegend_2, nrow=6),
             ncol=2,widths=c(7,1))

You'd need to fiddle a bit more to get the justification as in your expected output.

这篇关于控制ggplot2中多层图的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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