如何在ggplot中放置平均线?

时间:2017-12-05 22:43:07

标签: r ggplot2

我尝试做一个情节whith意味着逐行(在ggplot中),我有我的代码

ggplot(gama, aes(x = distancia, y= glipidoscmtejido, colour= estado)) + 
geom_point(position=position_dodge(.5), alpha= 1, size=3) +
geom_crossbar(data=gama,aes(x=distancia,ymin=mean(glipidoscmtejido), 
ymax=mean(glipidoscmtejido),y=mean(glipidoscmtejido),colour=estado), width = 0.5)

我得到了这个

enter image description here

但我需要distancia和estado的平均线。我该怎么做?

感谢。

1 个答案:

答案 0 :(得分:0)

如何使用stat_summary绘制每estado distancia# Generate some sample data set.seed(2017); df <- cbind.data.frame( x = rnorm(100), estado = sample(c("sana", "lesionada"), 100, replace = T), distancia = sample(c("0-1", "2.5-3.5", "5.6"), 100, replace = T)); require(ggplot2); ggplot(df, aes(x = distancia, y = x, colour = estado)) + geom_point(position = position_dodge(width = 0.3)) + stat_summary( fun.y = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..), position = position_dodge(width = 0.3), width = 0.25); 的均值:

$ cat file1
2    some
5    some
8    some
10   thing
15   thing
19   thing

$ awk 'max<$1 && $2=="some"{max=$1;b[$1]=1}END{for (i=1;i<=max;i++) print (i in b?1:0)}' file1
0
1
0
0
1
0
0
1

enter image description here