为复杂的情节构建一个手册图例 [英] Construct a manual legend for a complicated plot

查看:149
本文介绍了为复杂的情节构建一个手册图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何手动设置此图的图例。我真正想要的是一个简单的传说,右侧使用三种颜色,每个都有一个名字。

stack.imgur.com/xYukB.pngalt =

目前的代码如下所示:


$ (S1,S2,S3,S4,S5,S6,S7, S8,S9)#名称
b< -c(0.23,0.26,0.55,0.56,0.36,0.23,0.18,0.06,0.04)#mean t0
c< -c(0.64, 0.6,0.81,1.4,0.89,0.55,0.48,0.22,0.09)#mean t1
d <-c(0.20,0.23,0.52,0.53,0.33,0.20,0.15,0.04,0.03)#SD low t0
e< -c(0.26,0.29,0.58,.59,0.39,0.26,0.21,0.08,0.05)#SD high t0
f <-c(0.67,0.63,0.86,1.44,0.93 ,0.59,0.51,0.25,0.10)#SD high t1
g< -c(0.61,0.57,0.78,1.36,0.85,0.53,0.45,0.19,0.08)#SD low t1
h< -c(0.41,0.34,0.26,0.84,0.53,0.32,0.30,0.16,0.05)#绝对变化

data < - data.frame(a,b,c,d,e, f,g,h)

ggplot(data = data,aes(a))+
geom_bar(stat =identity,aes(y = h),fill =#62c76b ,颜色= #333333)+ #green
geom_line(aes(y = b,group = 1),size = 1.0,color =#f04546)+ #red
geom_point(aes(y = b ),size = 3,color =#f04546)+ #red
geom_errorbar(aes(ymin = d,ymax = e),color =#f04546,width = 0.1,size = .8)+
geom_line(aes(y = c,group = 1),size = 1.0,color =#3591d1)+ #blue
geom_point(aes(y = c),size = 3,color = #3591d1)+ #blue
geom_errorbar(aes(ymin = f,ymax = g),color =#3591d1,width = 0.1,size = .8)+
ylab(Symptom严重性)+ xlab(PHQ-9症状)+
ylim(0,1.6)+
theme_bw()+
主题(axis.title.x = element_text(size = 15) ,vjust = - 。2))+
theme(axis.title.y = element_text(size = 15,vjust = 0.3))


解决方案

您需要将属性映射到美学(aes语句中的颜色)才能生成图例。 $ b

  cols <-c(LINE1=#f04546,LINE2=#3591d1,BAR=#62c76b)
ggplot(data = data,aes(x = a))+
ge我们可以通过下面的例子来说明如何使用aes(y = b,group = 1,color =#) LINE1),size = 1.0)+ #red
geom_point(aes(y = b,color =LINE1),size = 3)+ #red
geom_errorbar(aes(ymin = d,ymax = e,color =LINE1),width = 0.1,size = .8)+
geom_line(aes(y = c,group = 1,color =LINE2),size = 1.0)+ #blue
geom_point(aes(y = c,color =LINE2),size = 3)+ #blue
geom_errorbar(aes(ymin = f,ymax = g,color =LINE2),width =size = .8)+
scale_colour_manual(name =Error Bars,values = cols)+ scale_fill_manual(name =Bar,values = cols)+
ylab(Symptom severity )+ xlab(PHQ-9症状)+
ylim(0,1.6)+
theme_bw()+
主题(axis.title.x = element_text(size = 15,vjust = - 。2))+
theme(axis.title.y = element_text(size = 15,vjust = 0.3))



我明白在哪里Rol并且来自于此,但由于这只是3个属性,并且由叠加条和误差条引起了复杂性,所以将数据保持为宽格式是合理的。通过使用geom_pointrange 可以稍微降低复杂度。






要更改原始错误栏图例的背景颜色,请添加 + theme(legend.key = element_rect(fill =white ,color =white))到绘图规范。要合并不同的图例,通常需要对所有元素都有一致的映射,但是它目前正在为我生成黑色背景的工件。我认为 guide = guide_legend(fill = NULL,color = NULL)会将图例的背景设置为null,但它没有。或许值得另一个问题。

  ggplot(data = data,aes(x = a))+ 
geom_bar(stat =identity,aes(y = h,fill =BAR,color =BAR))+ #green
geom_line(aes(y = b,group = 1,color =LINE1), (aes(y = b,color =LINE1,fill =LINE1),size = 3)+ #red
geom_errorbar(aes(ymin = d ,ymax = e,color =LINE1),width = 0.1,size = .8)+
geom_line(aes(y = c,group = 1,color =LINE2),size = 1.0)+ #blue
geom_point(aes(y = c,color =LINE2,fill =LINE2),size = 3)+ #blue
geom_errorbar(aes(ymin = f,ymax = g, color =LINE2),width = 0.1,size = .8)+
scale_colour_manual(name =Error Bars,values = cols,guide = guide_legend(fill = NULL,color = NULL))+
scale_fill_manual(name =Bar,values = cols,guide =none)+
ylab(Symptom severity)+ xlab(PHQ-9 symptoms)+
ylim(0 ,1.6)+
theme_bw()+
theme(axis.title.x = element_text(size = 15,vjust = - 。2))+
theme(axis.title.y =element_text(size = 15,vjust = 0.3))






为了摆脱图例中的黑色背景,您需要对 guide_legend 使用 override.aes 参数。这样做的目的是让你指定图例的一个特定的方面,可能没有被正确分配。

  ggplot(data = data,aes(x = a))+ 
geom_bar(stat =identity,aes(y = h,fill =BAR,color =BAR))+ #green
geom_line (aes(y = b,color =LINE1,fill =LINE1),aes(y = b,group = 1,color =LINE1),size = 1.0)+ #red
geom_point size = 3)+ #red
geom_errorbar(aes(ymin = d,ymax = e,color =LINE1),width = 0.1,size = .8)+
geom_line(aes(y = c,group = 1,color =LINE2),size = 1.0)+ #blue
geom_point(aes(y = c,color =LINE2,fill =LINE2),size = 3)+ #blue
geom_errorbar(aes(ymin = f,ymax = g,color =LINE2),width = 0.1,size = .8)+
scale_colour_manual(name =Error Bars,values = cols,
guide = guide_legend(override.aes = aes(fill = NA)))+
scale_fill_manual(name =Bar,values = cols,guide =none)+
ylab(症状严重程度)+ xlab(PHQ-9症状)+
ylim(0,1.6)+
theme_bw()+
theme(axis.title.x = element_text(size = 15,vjust = - 。2))+
theme(axis.title.y = element_text(size = 15,vjust = 0.3))


I cannot figure out how to manually set up a legend for this plot. All I really want is a simple legend to the right that uses the three colors and has a name next to each.

The current code looks like this:

a <-c("S1","S2","S3","S4","S5","S6","S7","S8","S9") #names
b <-c(0.23,0.26,0.55,0.56,0.36,0.23,0.18,0.06,0.04) #mean t0
c <-c(0.64,0.6,0.81,1.4,0.89,0.55,0.48,0.22,0.09) #mean t1
d <-c(0.20,0.23,0.52,0.53,0.33,0.20,0.15,0.04,0.03) #SD low t0
e <-c(0.26,0.29,0.58,.59,0.39,0.26,0.21,0.08,0.05) #SD high t0
f <-c(0.67,0.63,0.86,1.44,0.93,0.59,0.51,0.25,0.10) #SD high t1
g <-c(0.61,0.57,0.78,1.36,0.85,0.53,0.45,0.19,0.08) #SD low t1
h <-c(0.41,0.34,0.26,0.84,0.53,0.32,0.30,0.16,0.05) #absolute change

data <- data.frame(a,b,c,d,e,f,g,h)

ggplot(data=data,aes(a)) + 
  geom_bar(stat="identity", aes(y=h),fill="#62c76b",colour="#333333")+ #green
  geom_line(aes(y=b,group=1),size=1.0,colour="#f04546") +   #red
  geom_point(aes(y=b),size=3, colour="#f04546") +           #red
  geom_errorbar(aes(ymin=d, ymax=e), colour="#f04546", width=0.1, size=.8) + 
  geom_line(aes(y=c,group=1),size=1.0,colour="#3591d1") +   #blue 
  geom_point(aes(y=c),size=3, colour="#3591d1") +           #blue
  geom_errorbar(aes(ymin=f, ymax=g), colour="#3591d1", width=0.1, size=.8) + 
  ylab("Symptom severity") + xlab("PHQ-9 symptoms") +
  ylim(0,1.6) +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15, vjust=-.2)) +
  theme(axis.title.y = element_text(size = 15, vjust=0.3))

解决方案

You need to map attributes to aesthetics (colours within the aes statement) to produce a legend.

cols <- c("LINE1"="#f04546","LINE2"="#3591d1","BAR"="#62c76b")
ggplot(data=data,aes(x=a)) + 
  geom_bar(stat="identity", aes(y=h, fill = "BAR"),colour="#333333")+ #green
  geom_line(aes(y=b,group=1, colour="LINE1"),size=1.0) +   #red
  geom_point(aes(y=b, colour="LINE1"),size=3) +           #red
  geom_errorbar(aes(ymin=d, ymax=e, colour="LINE1"), width=0.1, size=.8) + 
  geom_line(aes(y=c,group=1,colour="LINE2"),size=1.0) +   #blue 
  geom_point(aes(y=c,colour="LINE2"),size=3) +           #blue
  geom_errorbar(aes(ymin=f, ymax=g,colour="LINE2"), width=0.1, size=.8) + 
  scale_colour_manual(name="Error Bars",values=cols) + scale_fill_manual(name="Bar",values=cols) +
  ylab("Symptom severity") + xlab("PHQ-9 symptoms") +
  ylim(0,1.6) +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15, vjust=-.2)) +
  theme(axis.title.y = element_text(size = 15, vjust=0.3))

I understand where Roland is coming from, but since this is only 3 attributes, and complications arise from superimposing bars and error bars this may be reasonable to leave the data in wide format like it is. It could be slightly reduced in complexity by using geom_pointrange.


To change the background color for the error bars legend in the original, add + theme(legend.key = element_rect(fill = "white",colour = "white")) to the plot specification. To merge different legends, you typically need to have a consistent mapping for all elements, but it is currently producing an artifact of a black background for me. I thought guide = guide_legend(fill = NULL,colour = NULL) would set the background to null for the legend, but it did not. Perhaps worth another question.

ggplot(data=data,aes(x=a)) + 
  geom_bar(stat="identity", aes(y=h,fill = "BAR", colour="BAR"))+ #green
  geom_line(aes(y=b,group=1, colour="LINE1"),size=1.0) +   #red
  geom_point(aes(y=b, colour="LINE1", fill="LINE1"),size=3) +           #red
  geom_errorbar(aes(ymin=d, ymax=e, colour="LINE1"), width=0.1, size=.8) + 
  geom_line(aes(y=c,group=1,colour="LINE2"),size=1.0) +   #blue 
  geom_point(aes(y=c,colour="LINE2", fill="LINE2"),size=3) +           #blue
  geom_errorbar(aes(ymin=f, ymax=g,colour="LINE2"), width=0.1, size=.8) + 
  scale_colour_manual(name="Error Bars",values=cols, guide = guide_legend(fill = NULL,colour = NULL)) + 
  scale_fill_manual(name="Bar",values=cols, guide="none") +
  ylab("Symptom severity") + xlab("PHQ-9 symptoms") +
  ylim(0,1.6) +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15, vjust=-.2)) +
  theme(axis.title.y = element_text(size = 15, vjust=0.3))


To get rid of the black background in the legend, you need to use the override.aes argument to the guide_legend. The purpose of this is to let you specify a particular aspect of the legend which may not be being assigned correctly.

ggplot(data=data,aes(x=a)) + 
  geom_bar(stat="identity", aes(y=h,fill = "BAR", colour="BAR"))+ #green
  geom_line(aes(y=b,group=1, colour="LINE1"),size=1.0) +   #red
  geom_point(aes(y=b, colour="LINE1", fill="LINE1"),size=3) +           #red
  geom_errorbar(aes(ymin=d, ymax=e, colour="LINE1"), width=0.1, size=.8) + 
  geom_line(aes(y=c,group=1,colour="LINE2"),size=1.0) +   #blue 
  geom_point(aes(y=c,colour="LINE2", fill="LINE2"),size=3) +           #blue
  geom_errorbar(aes(ymin=f, ymax=g,colour="LINE2"), width=0.1, size=.8) + 
  scale_colour_manual(name="Error Bars",values=cols, 
                      guide = guide_legend(override.aes=aes(fill=NA))) + 
  scale_fill_manual(name="Bar",values=cols, guide="none") +
  ylab("Symptom severity") + xlab("PHQ-9 symptoms") +
  ylim(0,1.6) +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15, vjust=-.2)) +
  theme(axis.title.y = element_text(size = 15, vjust=0.3))

这篇关于为复杂的情节构建一个手册图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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