R ggplot2 stat_summary图例表示组的附加值 [英] R ggplot2 stat_summary legend mean additional to groups

查看:44
本文介绍了R ggplot2 stat_summary图例表示组的附加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经绘制了整个研究人群的平均值(黑线),分别绘制了男性和女性的平均值.

I've plotted the mean for the whole study population (black line) and for men and women separately.

plotYYIR1<- ggplot(data=YYIR1Long, aes(x=TimeValue, y=YYIR1Value)) +
  labs(x="Week number", y="YYIR1 distance run (m)") +
  theme(plot.title = element_text(hjust = 0, vjust=0))+
  theme(legend.title=element_blank())+
  theme(legend.key.width = unit(1, "cm"))+
  stat_summary(fun.y = mean,geom = "point", size=2) + 
  stat_summary(fun.y = mean, geom = "line", size=0.7) +
  stat_summary(fun.y = mean,geom = "point", size=2, aes(shape=Sex,colour=Sex)) + 
  scale_shape_manual(values = c("Male"=17, "Female"=15))+
  stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour=Sex)) + 
  scale_colour_manual(values = c("#009CEF", "#CC0000"))+
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2)+
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour=Sex))
plotYYIR1

图例仅显示性别,有人可以帮我在整个组的图例中添加黑线和点吗?

The legend only shows the genders, could someone help me with adding the black line and points in the legend for the whole group?

推荐答案

您需要添加 aes()以获得黑色线条/点的图例.如果您希望图例适用于组合的线/形状,则可以通过在 scale_shape_manual 中添加 guide = F 来关闭形状的图例,然后使用 override.aes guides 中指定图例中的形状:

You need to add aes() to get a legend for the black line/points. If you want the legend to be for lines/shapes combined you can turn off the legend for shapes by adding guide = F to scale_shape_manual and then use override.aes in guides to specify the shapes in the legend:

 ggplot(data=YYIR1Long, aes(x=TimeValue, y=YYIR1Value)) +
  labs(x="Week number", y="YYIR1 distance run (m)") +
  theme(plot.title = element_text(hjust = 0, vjust=0))+
  theme(legend.title=element_blank())+
  theme(legend.key.width = unit(1, "cm"))+
  stat_summary(fun.y = mean,geom = "point", size=2, aes(colour = "mean")) + 
  stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour = "mean")) +
  stat_summary(fun.y = mean,geom = "point", size=2, aes(shape=Sex,colour=Sex)) + 
  scale_shape_manual(values = c("Male"=17, "Female"=15, "mean"=16), guide = F)+
  stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour=Sex)) + 
  scale_colour_manual(values = c("#009CEF", "#CC0000", "#000000"))+
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour = "mean"))+
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour=Sex)) +
  guides(colour = guide_legend(override.aes = list(shape = c("Male"=17, "Female"=15, "mean"=16)))) 

这篇关于R ggplot2 stat_summary图例表示组的附加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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