Footsteps on my way !
perl/linux/测序分析

R画箱图(boxplot)并标出均值

R画箱图时一般只显示出中位数和上四分位数和下四分位数,而均值也是衡量一个数据的重要指标,如何在boxplot线图中标出中位数呢?如下:

zx <- replicate (5, rnorm(50))
zx_means <- (colMeans(zx, na.rm = TRUE))
boxplot(zx, horizontal = FALSE, outline = FALSE)
points(zx_means, pch = 22, col = "darkgrey", lwd = 7)

如图:

示例2:(文件内容如下:)

2.1672    31.9662 0.5418  0.4478  26.4202 7.6126  0.4478
0.5418    31.9662 31.9662 0.4478  26.4202 18.8076 4.9258
0.5418    31.9662 21.672  20.5988 13.434  3.5824  1.3434
0.5418    24.381  7.5852  0.4478  25.5246 0.4478  0.4478
0.5418    20.0466 2.709   0.4478  11.195  17.4642 0.4478
0.5418    21.672  10.2942 0.4478  14.7774 17.4642 0.4478
3.2508    9.2106  0.5418  0.4478  6.717   22.8378 0.4478
0.5418    31.9662 18.4212 0.4478  17.912  20.5988 0.4478
0.5418    31.9662 0.5418  0.4478  26.4202 17.4642 17.4642
0.5418    35.7588 0.5418  0.4478  14.3296 17.4642 17.4642
4.8762    21.672  2.1672  21.4944 15.673  5.8214  21.4944
1.0836    21.672  24.381  0.4478  17.0164 2.6868  17.4642
3.7926    21.672  0.5418  0.4478  15.2252 5.3736  17.4642
1.0836    21.672  1.0836  23.2856 17.0164 17.4642 17.4642
1.6254    21.672  20.0466 25.5246 12.5384 17.4642 17.4642
3.2508    29.799  30.8826 2.6868  7.1648  17.4642 17.0164
1.6254    31.9662 21.672  0.4478  17.912  7.6126  17.0164
2.709    15.7122 31.9662 0.4478  9.8516  24.629  17.0164
1.6254    21.672  5.9598  0.4478  14.7774 17.4642 17.0164
2.1672    12.4614 5.418   0.4478  9.8516  2.239   17.0164
0.5418    21.672  16.7958 0.4478  14.7774 20.5988 17.0164
2.709    21.672  1.0836  6.2692  12.9862 11.6428 16.5686
0.5418    21.672  0.5418  20.151  13.434  17.0164 16.5686
mf<-read.table("/Users/yangl/Documents/work/feike_mathylation/cell_division_model_fitting/methyl_level_fitting/tm_version3/tm/all.txt",sep = "\t",header =F) 
par(mar = c(8, 2, 2, 2) + .2) #设置margin
boxplot(mf,outline = F,xaxt='n') 去掉横轴,同时去掉了刻度,所以不合适
boxplot(mf,outline = F,names = c("","","","","","","")) #去掉横轴标签
text(x=seq(1,7,by=1),y=-5, srt = 52, adj = 1, labels = c("11492_GATC","11492_CCWGG","11492_MTCGAK","k2044_GATC","k2044_CCWGG","k2044_MTCGAK","k2044_GRACRAC"),xpd = TRUE) #自己画很坐标标签,并倾斜52度
mf_means <- (colMeans(mf, na.rm = TRUE)) #获取mf每列的均值
points(mf_means, pch = 18, col = "darkgrey", lwd = 7) #在boxplot图上画均值点

尊重他人劳动成果,转载请注明出处:Bluesky's blog » R画箱图(boxplot)并标出均值

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址