抵消散点图上的平均值?

时间:2016-01-19 17:12:25

标签: r

the figure wth offset points but mean in the middle 我在同一个图上绘制了两组数据,通过使用不同的pch和偏移来区分这两组数据。我还想绘制两组数据的平均值,但到目前为止,我只能偏移数据点,而不是手段。这是我的代码

points(jitter(as.numeric(gen$genord)-0.1,0.1),ai$propaiacts, pch=15,col="dimgray",cex=1)
points(jitter(as.numeric(ugen$genord)+0.1,0.1),uai$propuaiacts, pch=6)
s=split(gen$propaiacts,gen$gencode)
points(jitter(sapply(s,  mean)+0.5,0.5),pch="__", cex=2)
s=split(ugen$propuaiacts,ugen$gencode)
points(jitter(sapply(s,  mean)-0.1,0.1),pch="__", cex=2)

这是相关数据:

dput(c(gen$genord,gen$propaiacts))
c(3, 1, 2, 3, 3, 1, 1, 2, 1, 2, 1, 2, 13.5986733, 6.6115702, 
9.2198582, 0.6001775, 1.0177719, 6.4348071, 10.0849649, 16.5116934, 
11.00971, 14.2514897, 4.366077, 7.3884464)
> dput(c(ugen$ugenord,ugen$propuaiacts))
c(3, 1, 2, 3, 3, 1, 1, 2, 1, 2, 1, 2, 1, 9.4512195, 6.3064133, 
7.2121554, 0.6486974, 1.0140406, 5.9735066, 10.076442, 12.5423729, 
9.6563923, 13.3744272, 4.4930535, 5.3341665, 21.0191083)

2 个答案:

答案 0 :(得分:0)

使用你的代码和数据集很困难,所以我将使用iris数据集,希望它能帮助你开始。作为基础R的替代,我使用了ggplot2。我只将数据从宽转换为长。然后我刚刚将position = position_dodge(width = 1)添加到geom_point()表达式中。为了添加每个组的平均值(黑点),我总结了数据集iris_melt。希望它能帮助你获得你想要的东西。

iris_melt <- melt(iris, id.vars=c("Species"))

iris_melt_s <- ddply(iris_melt, c("Species", "variable"), summarise,
               meanv = mean(value))

iris_melt <- melt(iris, id.vars=c("Species"))

ggplot(data=iris_melt, aes(x=variable, y=value, group=Species, color=Species, shape=Species)) + 
geom_point(position = position_dodge(width = 0.5)) + 
geom_point(data=iris_melt_s, aes(x=variable, y=meanv, group=Species, color=Species), color="black", position = position_dodge(width = 0.5))

enter image description here

答案 1 :(得分:0)

我意识到我可以简单地指定x上的类别数量然后移动它。它有点手册,但它现在有效。 s = split(ai $ propaiacts,ai $ recallord)points(c(1,2,3)-0.1,sapply(s,mean),pch =&#34; __&#34;,cex = 2)