beeswarm-蜜蜂图

一、beeswarm作为一维散点图包R包,可以生成点不重复的图,与stripchart的区别就是等值点不会重叠到一起,下图展示了stripchart与beeswarm图的区别:

stripchart(decrease ~ treatment, data = OrchardSprays, vertical = TRUE, log = "y", method = 'jitter', jitter = 0.2, cex = 1,pch = 16, col = rainbow(8),main = 'stripchart')

beeswarm(decrease ~ treatment, data = OrchardSprays, log = TRUE, pch = 16, col = rainbow(8),main = 'beeswarm')

  OrchardSprays是R自带的数据,decrease 和 treatment必须是数据的header

  pch:描点的样式

  jitter:把横坐标抖散(jitter),使本来都拥有相同坐标的点的横坐标稍有不同。jitter是基类函数{base},无需调用任何包

  

二、不同的beeswarm图:

  1.The color of individual points can be specified

 

install.packages(“beeswarm”) 
library(beeswarm)
data(breast)#必须先library(beeswarm),才能导入breast数据集
head(breast)
getwd()
setwd('/home/name/R')#自己设置路径
png('diff_color.png',width=400*2,height=400*2,res=72*2,type='cairo-png')
beeswarm(time_survival ~ ER, data=breast,pch=16, pwcol=1+as.numeric(event_survival),xlab="",ylab="Follow-up time (months)",labels = c("ER neg", "ER pos"))
legend("topright",legend=c("yes","no"),title="Censored",pch=16,col=1:2)
dev.off()

 

 

  2.Compare the four methods for arranging points#点的组织方式可以多种,如("swarm", "center", "hex", "square")

## Generate some random data
set.seed(123)
distro <- list(runif = runif(100, min = -3, max = 3), rnorm = rnorm(100))

png("arrange_points.png",width=600*2,height=600*2,res=72*2,type='cairo-png')
par(mfrow=c(2,2))#两行两列画布
for (m in c("swarm", "center", "hex", "square")) {
    beeswarm(distro, col = 2:3, pch = 16,method = m, 
    main = paste('method = "', m, '"', sep = ''))
}

dev.off()

 

  3.Combining beeswarms with "boxplot" or "bxplot"

  

png("box_plus_beeswarm.png",width=600*2,height=600*2,res=72*2,type='cairo-png')
par(mfrow=c(1,2))
boxplot(len ~ dose, data = ToothGrowth,outline = FALSE,main = 'boxplot + beeswarm')
#outline = FALSE :avoid double-plotting outliers, if any
beeswarm(len ~ dose, data = ToothGrowth,col = 4, pch = 16, add = TRUE)

beeswarm(len ~ dose, data = ToothGrowth,col = 4, pch = 16,main = 'beeswarm + bxplot')
bxplot(len ~ dose, data = ToothGrowth, add = TRUE)
dev.off()

  4.New arguments "side" and "priority"

 

png("side_priority.png",width=600*2,height=600*2,res=72*2,type='cairo-png')
par(mfrow=c(2,3))
beeswarm(distro, col = 2:4, main = 'Default')

beeswarm(distro, col = 2:4, side = -1, main = 'side = -1')

beeswarm(distro, col = 2:4, side = 1, main = 'side = 1')

beeswarm(distro, col = 2:4, priority = "descending", main = 'priority = "descending"')

beeswarm(distro, col = 2:4, priority = "random", main = 'priority = "random"')

beeswarm(distro, col = 2:4, priority = "density", main = 'priority = "density"')

dev.off()

 

  参考:http://www.cbs.dtu.dk/~eklund/beeswarm/

 

 

 

 

  

 

posted @ 2018-04-11 16:47  1直在路上1  阅读(1665)  评论(0编辑  收藏  举报