将平均线添加到ggplot [英] add mean line to ggplot
本文介绍了将平均线添加到ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想建立在这个例子的条件线颜色在斜坡上添加一条平均线(从平均值t = 1到平均值t = 2的线)。
I'd like to build on this example of conditioning line color on slope to add a line for the mean (line from mean t=1 to mean t=2).
library(dplyr)
set.seed(205)
dat = data.frame(t=rep(1:2, each=10),
pairs=rep(1:10,2),
value=rnorm(20))
ggplot(dat %>% group_by(pairs) %>%
mutate(slope = (value[t==2] - value[t==1])/(2-1)),
aes(t, value, group=pairs, colour=slope > 0)) +
geom_point() +
geom_line()
我尝试添加
I tried adding stat_summary(fun.y=mean, geom="line")
without any luck.
计算失败
stat_summary()
:'what'必须是一个函数或字符串
Computation failed in
stat_summary()
: 'what' must be a function or character string
推荐答案
我没有t得到你描述的错误(用ggplot2 v 2.2.0),但为了得到想要的结果,我必须重写你在主图中指定的分组:
I didn't get the error you describe (with ggplot2 v 2.2.0), but in order to get the desired result I did have to override the grouping you specified in the main plot:
stat_summary(fun.y=mean,geom="line",lwd=2,aes(group=1))
这篇关于将平均线添加到ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文