将错误栏应用于ggplot中的平均值

时间:2016-06-01 14:26:41

标签: r ggplot2 errorbar

我已经制作了胰岛素浓度的ggplot。在不同的时间点(OGTT)进行两种不同的治疗。之后我添加了显示平均值的点:

p<-ggplot(data=data2, aes(x = factor(OGTT), y = Insulin, group = StudyID, color=StudyID)) + 
  geom_line() +
  facet_grid(End.start ~Treatment)+ 
  stat_summary(aes(group = Treatment), geom = "point", fun.y = mean, shape = 16, size = 2) 

我的问题是:

  1. 如何将误差标添加到平均值点?
  2. 如何避免情节显示任何传说?

1 个答案:

答案 0 :(得分:1)

ggplot2 cookbook可以解答大多数相当基本的问题。

要添加误差线,请分别计算误差线的上限和下限值。然后使用geom_errorbar()绘图。

http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/

要删除所有图例,请使用

theme(legend.position="none")

为了获得更多控制权,还有许多其他选择。

http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/