简体   繁体   English

ggplot2 为两个数据系列添加手动图例

[英]ggplot2 add manual legend for two data series

I have this dataframe:我有这个 dataframe:

     Control    Stress days  sd_control  sd_stress
X1 0.9702100 0.9343627   X1 0.001900535 0.07035645
X2 0.9666619 0.8595523   X2 0.014946893 0.04066567
X3 0.9165654 0.7160598   X3 0.072655343 0.07025344
X4 0.9208237 0.6668044   X4 0.050870831 0.08736982
X5 0.8766547 0.7660685   X5 0.073588197 0.04868614
X6 0.9599553 0.7937444   X6 0.041559836 0.05326769
X7 0.9736297 0.8188934   X7 0.003817743 0.06272428

and based on this data I've done this plot:并根据这些数据,我完成了这个 plot:

在此处输入图像描述

With the following code:使用以下代码:

significance <- data.frame(days=c("X2","X3","X4","X6"),value=c(1.02,1.02,1.02,1.02))
ggplot(my_data, aes(x=days,y=Control,group=1)) +
  geom_errorbar(aes(ymax = Control-sd_control, ymin = Control+sd_control),
                width=0.2, size=0.5) +
  geom_errorbar(aes(ymax = Stress-sd_stress, ymin = Stress+sd_stress),
                width=0.2, size=0.5) +
  geom_point(shape=23,color='gray45',fill='gray45',size=4) + 
  geom_line(color='gray45',size=1) +
  geom_point(data=my_data,aes(x=days,y=Stress),size=4,shape=22,fill='gray',color='gray',
             show.legend = TRUE) +
  geom_line(data = my_data, aes(x=days,y=Stress),color='gray',size=1) +
  geom_point(data=significance, aes(x=days,y=value),shape='*',size=6) +
  labs(x='\nDAT',y='RWC\n') +
  scale_y_continuous(labels = percent_format(accuracy = 1),limits = c(0.5,1.04), 
                     expand = c(0,0), breaks = seq(from=0.5,to=1,by=0.05)) +
  scale_x_discrete(expand = c(0.07, 0),labels = c(0,7,14,21,27,35,42)) +
  ggtitle('Relative Water Content\n') +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5),
        panel.background = element_rect(fill = 'white'),
        plot.title = element_text(hjust = 0.5,family = 'Calibri',face='bold'),
        axis.title = element_text(family = 'Calibri',face = 'bold'),
        axis.text = element_text(family = 'Calibri')
        )

I want to add a legend in the bottom-right on the plot that describres the Control and Stress Treatmentes with the same shape of the points.我想在 plot 的右下角添加一个图例,描述具有相同形状点的控制和应力处理。 I've tried several approaches that I've found here as set a color vector and scale_colour_manual attributes but none of them worked.我尝试了几种我在这里找到的方法来设置颜色向量和 scale_colour_manual 属性,但它们都不起作用。 Any suggestion?有什么建议吗?

The issue is that you use the color , fill and shape arguments.问题是您使用colorfillshape arguments。

  • To get a legend you have to map on aesthetics, ie inside aes() .要获得传奇,您必须在美学上使用 map,即在aes()内部。
  • After doing so ggplot will add lgends(s) automatically and you can apply scale_xxx_manual to get the desired colors, fill and shapes.这样做之后,ggplot 将自动添加 lgends(s),您可以应用scale_xxx_manual来获得所需的 colors、填充和形状。
  • However, as this results in 3 legends (was not able to figure out why the merging of the legends failed) I use guides to keep only one of them and guide_legend to style the legend.但是,由于这会导致 3 个图例(无法弄清楚为什么图例的合并失败),我使用guides仅保留其中一个,并guide_legend来设置图例的样式。 Try this:尝试这个:
library(ggplot2)
library(scales)

ggplot(my_data, aes(x=days, group=1)) +
  geom_errorbar(aes(ymax = Control-sd_control, ymin = Control+sd_control),
                width=0.2, size=0.5) +
  geom_errorbar(aes(ymax = Stress-sd_stress, ymin = Stress+sd_stress),
                width=0.2, size=0.5) +
  geom_point(aes(y=Control, color = "Control", fill = "Control", shape = "Control"), size=4) + 
  geom_line(aes(y=Control, color = "Control"),size=1) +
  geom_point(aes(y=Stress, color = "Stress", fill = "Stress", shape = "Stress"), size=4) +
  geom_line(aes(y=Stress, color = "Stress"), size=1) +
  geom_point(data=significance, aes(y=value),shape='*',size=6) +
  scale_color_manual(values = c("Control" = 'gray45', "Stress" = 'gray') ) +
  scale_fill_manual(values = c("Control" = 'gray45', "Stress" = 'gray') ) +
  scale_shape_manual(values = c("Control" = 23, "Stress" = 22)) +
  guides(shape = FALSE, fill = FALSE, 
         color = guide_legend(override.aes = list(shape =  c("Control" = 23, "Stress" = 22),
                                                  fill = c("Control" = 'gray45', "Stress" = 'gray')))) +
  labs(x='\nDAT',y='RWC\n') +
  scale_y_continuous(labels = percent_format(accuracy = 1),limits = c(0.5,1.04), 
                     expand = c(0,0), breaks = seq(from=0.5,to=1,by=0.05)) +
  scale_x_discrete(expand = c(0.07, 0), labels = c(0,7,14,21,27,35,42)) +
  ggtitle('Relative Water Content\n') +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5),
        panel.background = element_rect(fill = 'white'),
        plot.title = element_text(hjust = 0.5,family = 'Calibri',face='bold'),
        axis.title = element_text(family = 'Calibri',face = 'bold'),
        axis.text = element_text(family = 'Calibri')
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM