Construct a manual legend for a complicated plot
我无法弄清楚如何为此图手动设置图例。我真正想要的只是一个简单的图例,该图例使用三种颜色并在每种颜色旁边都有一个名称。
当前代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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)) |
您需要将属性映射到美学(es语句中的颜色)以生成图例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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)) |
我知道Roland的来源,但是由于这仅是3个属性,并且由于条形图和错误条形的叠加而产生了复杂性,因此按原样保留宽格式的数据可能是合理的。使用geom_pointrange可以稍微降低复杂度。
要更改原始错误栏图例的背景颜色,请在绘图说明中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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)) |
要摆脱图例中的黑色背景,您需要对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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)) |
如果您正在努力更改
我们将尝试扩展学习的模式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | cols <- c("LINE1"="#f04546","LINE2"="#3591d1","BAR"="#62c76b") line_types <- c("LINE1"=1,"LINE2"=3) ggplot(data=data,aes(x=a)) + geom_bar(stat="identity", aes(y=h,fill ="BAR"))+ #green geom_line(aes(y=b,group=1, colour="LINE1", linetype="LINE1"),size=0.5) + #red geom_point(aes(y=b, colour="LINE1", fill="LINE1"),size=2) + #red geom_line(aes(y=c,group=1,colour="LINE2", linetype="LINE2"),size=0.5) + #blue geom_point(aes(y=c,colour="LINE2", fill="LINE2"),size=2) + #blue scale_colour_manual(name="Error Bars",values=cols, guide = guide_legend(override.aes=aes(fill=NA))) + scale_linetype_manual(values=line_types)+ 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)) |
但是,我们得到的是以下结果:
问题是
注意,我们没有给方法
此处起作用的技巧是为它命名与命名
更具体地说,如果将相应的行更改为以下内容,则会得到所需的结果:
1 | scale_linetype_manual(name="Error Bars",values=line_types) |
现在,以相同的想法很容易更改行的大小。
请注意,