如何为一个beeswarm情节添加平均值

时间:2017-06-08 17:25:19

标签: r

这是数据集(d2):

    WT.hypo  WT.hyper   KO.hypo KO.hyper
1 11.996507  2.406066 10.903488 7.595285
2 10.992267  2.192558  9.399490 7.184496
3 16.797177 11.088748  6.221103 4.293984
4  9.918818  2.937259  6.317434 3.319860
5        NA        NA  5.918438 2.914264

这是脚本:

#beeswarm
beeswarm(d2, method="swarm", col = c("black"), pch = 16, cex.axis = 1, cex = 1 ) 

#add mean
m <- mean(d2, na.rm=T)
segments(0.9,m[[1]],1.1,m[[1]], lwd = 2, lty = 3, col = "black")

似乎“m&lt; - mean(d2,na.rm = T)”中缺少一些参数

数据集:

d2=structure(list(WT.hypo = c(11.996507, 10.992267, 16.797177, 9.918818, 
NA), WT.hyper = c(2.406066, 2.192558, 11.088748, 2.937259, NA
), KO.hypo = c(10.903488, 9.39949, 6.221103, 6.317434, 5.918438
), KO.hyper = c(7.595285, 7.184496, 4.293984, 3.31986, 2.914264
)), .Names = c("WT.hypo", "WT.hyper", "KO.hypo", "KO.hyper"), class = "data.frame", row.names = c(NA, 
-5L))

2 个答案:

答案 0 :(得分:3)

首先,你需要制作一个手段向量:

> means=apply(d2, 2, function(x) mean(na.omit(x))) #don't forget na.omit
> means
  WT.hypo  WT.hyper   KO.hypo  KO.hyper 
12.426192  4.656158  7.751991  5.061578 

然后你可以添加以下行:

> segments(0.9, means[[1]], 1.1, means[[1]], lwd=2, lty=3, col="black")
> segments(1.9, means[[2]], 2.1, means[[2]], lwd=2, lty=3, col="black")
> segments(2.9, means[[3]], 3.1, means[[3]], lwd=2, lty=3, col="black")
> segments(3.9, means[[4]], 4.1, means[[4]], lwd=2, lty=3, col="black")

enter image description here

我建议您考虑使用bxplot()中的内置beeswarm函数;它更容易。

答案 1 :(得分:0)

正如@Matt所说,你可以这样做:

beeswarm(d2, method="swarm", col = c("black"), pch = 16, cex.axis = 1, cex = 1 ) 
bxplot(d2, add = TRUE, probs = 0.5)

我的问题是如何绘制这样的图片,3行的长度不一样。

enter image description here