一种方法是在绘制新数据框之前重新计算平均值和标准差值。另一种方法是定义自己的stat_
功能。这是stat_summary()
帮助页面中的修改示例。这将计算每年每种治疗的平均值和置信区间,并将其绘制为geom="pointrange"
。用stat_summary()
和添加行geom="line"
。
stat_sum_df <- function(fun, geom="crossbar", ...) {
stat_summary(fun.data=fun, geom=geom, width=0.2, ...)
}
ggplot(test, aes(x = factor(Year), y = DV, colour=Grp,group=Grp)) +
stat_sum_df("mean_cl_normal",geom="pointrange")+
stat_summary(fun.y="mean",geom="line")

更新
要获得标准错误,您必须创建新功能。我命名它stat_mean_sd()
。现在在里面使用这个函数stat_summary()
。
stat_mean_sd<-function(x){
cal.df<-data.frame(
y=mean(x),
ymin=mean(x)-sd(x)/sqrt(length(x)),
ymax=mean(x)+sd(x)/sqrt(length(x)))
return(cal.df)
}
ggplot(test, aes(x = factor(Year), y = DV, colour=Grp,group=Grp)) +
stat_summary(fun.data=stat_mean_sd,geom="pointrange")+
stat_summary(fun.y="mean",geom="line")
