簡體   English   中英

R ggplot2 stat_summary圖例表示組的附加值

[英]R ggplot2 stat_summary legend mean additional to groups

我已經畫出了整個研究人群的平均值(黑線),分別繪制了男性和女性的平均值。

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

圖例僅顯示性別,有人可以幫我在整個組的圖例中添加黑線和點嗎?

在此處輸入圖片說明

您需要添加aes()以獲取黑色線條/點的圖例。 如果你想傳說是線/形狀組合可以通過添加關閉對形狀的傳說guide = Fscale_shape_manual然后用override.aesguides指定圖例中的形狀:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM