R语言可视化(二十):蜂群图绘制

20. 蜂群图绘制


清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/20beeswarm/")

使用beeswarm包绘制蜂群图

# 安装并加载所需的R包
#install.packages("beeswarm")
library(beeswarm)

# 查看示例数据
data(breast)
head(breast)
##             ER      ESR1     ERBB2 time_survival event_survival
## 100.CEL.gz neg  8.372133 13.085894            39              1
## 103.CEL.gz pos 10.559356  9.491683            97              0
## 104.CEL.gz pos 12.299905  9.599574            11              1
## 105.CEL.gz pos 10.776632  9.681747            99              0
## 106.CEL.gz pos 10.505124  9.436763            40              1
## 107.CEL.gz neg 10.377741  8.695576            94              0

# 使用beeswarm函数绘制蜂群图
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)
image.png
# 构建数据集
distributions <- list(runif = runif(200, min = -3, max = 3), 
                      rnorm = rnorm(200),
                      rlnorm = rlnorm(200, sdlog = 0.5))
head(distributions)
## $runif
##   [1] -2.27342130 -1.18900588  0.10207237  2.48641084  0.02012957
##   [6] -1.76829008 -1.12047958 -2.96815139 -0.31209029 -0.20493307
##  [11]  0.53143012  2.67035036  0.59628722  1.92263701  2.11544601
##  [16] -0.17847743 -0.19457512 -0.35261991  0.20157680 -0.58535626
##  [21] -0.94345311  2.24618442  1.42956983 -0.01546641  1.92465462
##  [26]  1.85646013 -1.96350925  2.88141264 -1.58954564 -2.59488466
##  [31] -0.10168001 -1.82031715 -1.61198531  2.07702608  0.37988021
##  [36]  1.54899753  1.07041703 -2.03624578 -1.28838168 -2.30341964
##  [41] -1.10220164  1.04159956  1.48231590 -2.53509987  2.95108409
##  [46] -1.16708612 -2.17746176 -2.96820675 -1.35705811 -0.69694647
##  [51] -0.65326826  1.06238888  2.68613540 -1.61345015  2.38956040
##  [56]  1.64909828 -2.75083220 -0.39655812 -2.53444941  1.70743462
##  [61]  2.48161747  0.38436338 -1.62120396  1.27086364  0.18473899
##  [66] -2.45624898  2.84737775  0.69877255 -1.59883214  2.84328497
##  [71] -2.71260910  2.12899645 -0.55391466  2.56525514  1.60610397
##  [76]  1.89470114  0.24974871  1.29468379 -1.68490954  0.24718837
##  [81]  2.57114959 -1.05296498  0.72760152 -0.35293521  1.37158913
##  [86] -1.15804483 -1.77093593  2.31006434  1.30922547 -1.26082340
##  [91] -1.80740622  0.70394994 -1.42286040  0.47898702  2.77591797
##  [96] -0.75701705 -2.51006922  2.62659029  2.12551589  0.88384133
## [101] -0.78625914 -0.68082186 -2.92262263 -0.88899955 -0.18300252
## [106] -2.88269501  2.81048886 -2.05658480  1.52128352 -1.48870146
## [111]  0.41419230 -1.15223857  1.60080245  0.62031479 -0.17486794
## [116] -2.27752586 -1.86405359 -2.18210835  0.77318548 -1.19686210
## [121]  1.76915143  2.64287847  0.29854052 -0.43338978 -1.35907366
## [126] -2.11887460 -2.72545120  1.99629706 -1.22257210 -0.11310617
## [131]  2.54911076 -2.67415839  0.23629662  1.87552709  2.10443199
## [136] -1.86874793  1.98237107  2.09316895 -2.29578630  2.58669845
## [141] -2.90908560  0.78919034 -1.74099982 -2.64480113 -2.29338438
## [146]  1.55922183 -1.16395726 -2.58925043 -1.15451330 -1.98600813
## [151]  0.06535301  1.36614752  0.96644383  2.45370577 -1.36831890
## [156] -2.36704072  2.45158066 -2.66900372 -0.33322824  1.29933735
## [161] -1.40522057  2.75957952 -2.37326739 -2.12440636  2.83057601
## [166] -0.59730999  1.63675029 -2.12247490 -0.02721347 -0.37537194
## [171] -2.23926139 -0.99887838 -2.12109716 -1.32519642 -2.41842746
## [176] -2.81095168 -0.02653292  0.75535380 -1.81941949 -2.11793511
## [181] -0.69943062 -0.16928516  0.97912430 -2.85847098  2.19261275
## [186] -0.74274609  0.80193414  1.73658828 -0.82983463 -0.85846183
## [191] -0.49244769 -1.67097068  2.72155447 -2.42111947 -2.38387189
## [196]  0.58730473 -1.17695543  2.98118484  2.64369963 -2.64893453
## 
## $rnorm
##   [1]  0.65168849  0.23831240  0.01196427  1.28600981 -2.40623872
##   [6] -0.59107471  1.10613620  0.34624722 -0.15931110 -1.14132719
##  [11]  0.30034787 -0.07139573 -1.19490162  1.30202484  0.48298588
##  [16] -1.58410136 -1.87010323  0.32483536 -0.86726250  0.74417774
##  [21]  0.35545302 -0.65339836 -0.19188768  0.07706257  0.05098065
##  [26]  0.53924913  1.92898606 -1.94213042  1.33925572  0.31339775
##  [31] -0.26012295  0.56386918 -0.05940329 -0.87635555  1.02471751
##  [36] -0.24652690  0.45412186 -1.30751056  1.54999336  0.25797917
##  [41] -1.69519268 -0.13427878  0.35874717  0.37106054 -0.28187651
##  [46]  0.73294626  0.21845059 -0.70012657  1.63911043  0.31451866
##  [51]  0.14268997  0.66756336 -1.32038861 -1.21383898  1.80207501
##  [56]  0.35809301 -1.82925465 -1.15215338  1.38047468  0.30305593
##  [61] -0.46945828 -0.63588974 -0.38799925  0.37712393 -0.24997514
##  [66]  0.01200651  0.64629694  2.02652658 -0.62783308 -2.04965879
##  [71] -0.98809761  0.07520611  3.05226268  2.54195133  0.45679411
##  [76] -1.57473929  0.38879966  0.28757516  0.05290903 -0.03137800
##  [81] -0.88744671 -1.34331796 -0.48391629  0.70541977 -0.80224778
##  [86]  1.35287853  0.45580046  1.00297988  0.41465031  0.97502857
##  [91]  2.09665942 -1.00732611 -0.72330090 -0.19675224  2.89442299
##  [96]  0.80294212  1.03223725  1.11097159 -1.54682335 -0.62625606
## [101]  0.68397693 -0.98589321 -0.53322247 -0.62105754  0.43201184
## [106] -0.57388322 -0.34675376 -3.35288364 -1.46998567 -0.62512679
## [111]  0.92573978  0.54078071 -0.64561927  0.13692961  0.06104394
## [116] -1.16822275  0.84484124 -0.29115458 -1.59999064 -1.19446360
## [121]  0.01433903 -0.44347507 -0.61865874 -0.17609964  0.32841363
## [126] -0.60679828  2.83902573  0.41734786  0.34780494  0.29214522
## [131]  0.66150101  0.98148641  1.57471929  0.36470458  0.43285216
## [136] -0.09295342  1.59988681 -0.98346955 -0.46130393  0.21742451
## [141] -1.78165311  1.65649424  0.32930764 -0.78579468 -2.11941282
## [146]  1.45236584  0.31064939 -0.85122674  1.76101897 -1.36611718
## [151]  1.84875706 -0.14397341 -0.69057620 -0.68280446 -0.09099254
## [156]  0.23424194 -2.15442087 -0.45007788 -1.97136857  0.12185722
## [161] -1.07218157 -0.60053514 -0.54051415 -1.09351439 -0.34412991
## [166] -0.22741474 -0.51430179  0.05360325  1.88300669 -1.21270983
## [171]  0.19468872 -0.77689439 -0.91643640 -0.65218721 -0.21471610
## [176] -1.55288556 -0.32585005 -0.36372859 -0.46204618  0.99250060
## [181]  0.49994490 -1.03916691  0.52955414 -0.94735455  0.68580576
## [186]  0.34181005 -0.03896710  0.18710580 -0.27666791 -0.84691980
## [191]  0.50796588 -0.02949926  0.05581683  2.01028405 -0.61829879
## [196] -1.24907644 -0.09560583  0.20480474  2.43691332  0.72549553
## 
## $rlnorm
##   [1] 1.4180215 0.9949893 1.2095499 0.5556711 2.8456954 0.7542017 0.8869332
##   [8] 0.3929428 1.7824585 0.8578438 0.7450027 2.0012445 0.9222689 0.8507828
##  [15] 1.4291946 0.5356243 1.8302340 1.5275254 1.6394231 1.7578815 1.4312519
##  [22] 1.2614112 1.4322253 1.5636101 1.5106413 2.3727410 0.6619246 1.0472863
##  [29] 1.3376214 0.8123351 0.8775919 0.6487132 0.6398750 0.6789929 0.6676162
##  [36] 0.8030561 1.1987341 0.8220363 0.6600954 0.8488341 0.4667499 0.5342053
##  [43] 1.1162720 0.5101835 1.0856777 1.5935279 1.7699189 1.5699195 2.1842268
##  [50] 0.4498118 0.5280608 0.7646684 1.0554103 0.7592580 1.0901559 0.6557038
##  [57] 0.8794793 1.0603791 1.4582320 0.9479492 1.5070970 0.6798005 0.5017900
##  [64] 1.1348251 1.7835876 0.4357698 1.3643004 1.3053056 1.2654007 0.5473514
##  [71] 2.1864139 0.7594870 0.6156391 2.7250001 1.0054539 3.0897965 0.7491639
##  [78] 0.2426790 0.8795551 1.4644277 0.8854342 0.9939607 0.7987942 0.7481332
##  [85] 1.8616133 0.7841885 0.5208239 1.3061559 0.9200854 0.7152498 0.7064833
##  [92] 0.6707401 0.9356943 0.6323643 0.6292022 0.6875065 0.9643136 0.8779377
##  [99] 0.6932219 0.6865461 1.8310179 1.4742724 1.2115138 2.2221975 1.1983138
## [106] 1.3688784 1.2895110 2.0687679 2.6769289 0.5259231 0.3041131 1.0541713
## [113] 1.1218921 2.0191431 0.9936532 1.9340580 1.0711772 1.0444810 1.0261481
## [120] 0.5294632 1.3136951 0.5384892 1.4269396 1.6812430 0.8125970 1.2026204
## [127] 1.0220618 0.5245410 1.2838802 2.2030659 1.2609909 1.1861876 0.6115918
## [134] 1.6431244 1.1483429 1.0359047 0.8920842 0.8028762 0.7747547 1.0884591
## [141] 0.8185835 3.1236314 0.5380213 0.6690222 1.4020351 0.8615262 1.0765957
## [148] 1.7736696 1.0273700 1.0803124 0.9207693 0.4634630 1.1210904 0.9298725
## [155] 1.3379793 0.4569237 1.9919562 1.8117338 0.2431258 0.5162803 0.7153854
## [162] 1.8503029 0.2643302 0.6136971 1.3140907 0.7337556 2.3274112 0.7754586
## [169] 0.4465594 1.1678464 0.6261724 0.9174300 1.4529425 1.0351601 0.8499914
## [176] 1.8094545 1.2384073 2.0756710 1.7120115 1.9649799 0.5449036 1.2395233
## [183] 0.5451760 1.2948469 0.5684139 0.4601785 1.7769395 2.3370098 1.2654116
## [190] 0.5203092 2.1489925 0.6257352 1.0893499 0.3583439 0.5190914 0.9715436
## [197] 1.9893117 2.0029403 1.4266857 0.5357041

beeswarm(distributions, col = 2:4)
image.png
## Demonstrate 'pwcol' with the list interface 
myCol <- lapply(distributions, function(x) cut(x, breaks = quantile(x), labels = FALSE))
myCol
## $runif
##   [1]  1  2  3  4  3  1  2  1  3  3  3  4  3  4  4  3  3  2  3  2  2  4  3
##  [24]  3  4  4  1  4  2  1  3  1  2  4  3  4  3  1  2  1  2  3  3  1  4  2
##  [47]  1 NA  2  2  2  3  4  2  4  4  1  2  1  4  4  3  2  3  3  1  4  3  2
##  [70]  4  1  4  2  4  4  4  3  3  2  3  4  2  3  2  3  2  1  4  3  2  1  3
##  [93]  2  3  4  2  1  4  4  3  2  2  1  2  3  1  4  1  4  2  3  2  4  3  3
## [116]  1  1  1  3  2  4  4  3  2  2  1  1  4  2  3  4  1  3  4  4  1  4  4
## [139]  1  4  1  3  1  1  1  4  2  1  2  1  3  3  3  4  2  1  4  1  2  3  2
## [162]  4  1  1  4  2  4  1  3  2  1  2  1  2  1  1  3  3  1  1  2  3  3  1
## [185]  4  2  3  4  2  2  2  2  4  1  1  3  2  4  4  1
## 
## $rnorm
##   [1]  4  3  3  4  1  2  4  3  2  1  3  2  1  4  3  1  1  3  1  4  3  2  2
##  [24]  3  3  4  4  1  4  3  2  4  2  1  4  2  3  1  4  3  1  2  3  3  2  4
##  [47]  3  1  4  3  3  4  1  1  4  3  1  1  4  3  2  2  2  3  2  3  4  4  2
##  [70]  1  1  3  4  4  3  1  3  3  3  2  1  1  2  4  1  4  3  4  3  4  4  1
##  [93]  1  2  4  4  4  4  1  2  4  1  2  2  3  2  2 NA  1  2  4  4  2  3  3
## [116]  1  4  2  1  1  3  2  2  2  3  2  4  3  3  3  4  4  4  3  3  2  4  1
## [139]  2  3  1  4  3  1  1  4  3  1  4  1  4  2  1  1  2  3  1  2  1  3  1
## [162]  2  2  1  2  2  2  3  4  1  3  1  1  2  2  1  2  2  2  4  3  1  4  1
## [185]  4  3  2  3  2  1  4  2  3  4  2  1  2  3  4  4
## 
## $rlnorm
##   [1]  3  2  3  1  4  2  2  1  4  2  2  4  2  2  3  1  4  4  4  4  4  3  4
##  [24]  4  4  4  1  3  3  2  2  1  1  1  1  2  3  2  1  2  1  1  3  1  3  4
##  [47]  4  4  4  1  1  2  3  2  3  1  2  3  4  2  4  1  1  3  4  1  3  3  3
##  [70]  1  4  2  1  4  2  4  2 NA  2  4  2  2  2  2  4  2  1  3  2  2  2  1
##  [93]  2  1  1  2  2  2  2  2  4  4  3  4  3  3  3  4  4  1  1  3  3  4  2
## [116]  4  3  3  3  1  3  1  3  4  2  3  2  1  3  4  3  3  1  4  3  3  2  2
## [139]  2  3  2  4  1  1  3  2  3  4  3  3  2  1  3  2  3  1  4  4  1  1  2
## [162]  4  1  1  3  2  4  2  1  3  1  2  4  3  2  4  3  4  4  4  1  3  1  3
## [185]  1  1  4  4  3  1  4  1  3  1  1  2  4  4  3  1

beeswarm(distributions, pch = 16, pwcol = myCol)
# 添加图例
legend("topright", legend = 1:4, pch = 16, col = 1:4, title = "Quartile")
image.png
## Compare the 4 methods
# 使用method参数设置蜂群点分布的方法
op <- par(mfrow = c(2,2))
for (m in c("swarm", "center", "hex", "square")) {
  beeswarm(distributions, method = m, 
           main = paste0("method = ", m), 
           pch = 16, pwcol = myCol)
}
par(op)
image.png
## Demonstrate the 'corral' methods
# 使用corral参数调整组外离群点的分布
op <- par(mfrow = c(2,3))
beeswarm(distributions, col = 2:4, 
         main = 'corral = "none" (default)')
beeswarm(distributions, col = 2:4, corral = "gutter", 
         main = 'corral = "gutter"')
beeswarm(distributions, col = 2:4, corral = "wrap", 
         main = 'corral = "wrap"')
beeswarm(distributions, col = 2:4, corral = "random", 
         main = 'corral = "random"')
beeswarm(distributions, col = 2:4, corral = "omit", 
         main = 'corral = "omit"')  
par(op)
image.png
## Demonstrate 'side' and 'priority'
# 调整蜂群点的排序和分布形式
op <- par(mfrow = c(2,3))
beeswarm(distributions, col = 2:4, 
         main = 'Default')
beeswarm(distributions, col = 2:4, side = -1, 
         main = 'side = -1')
beeswarm(distributions, col = 2:4, side = 1, 
         main = 'side = 1')
beeswarm(distributions, col = 2:4, priority = "descending", 
         main = 'priority = "descending"')
beeswarm(distributions, col = 2:4, priority = "random", 
         main = 'priority = "random"')  
beeswarm(distributions, col = 2:4, priority = "density", 
         main = 'priority = "density"')  
par(op)
image.png

使用ggbeeswarm包绘制蜂群图

#install.packages("ggbeeswarm")
library(ggbeeswarm)

# 查看示例数据
head(breast)
##             ER      ESR1     ERBB2 time_survival event_survival
## 100.CEL.gz neg  8.372133 13.085894            39              1
## 103.CEL.gz pos 10.559356  9.491683            97              0
## 104.CEL.gz pos 12.299905  9.599574            11              1
## 105.CEL.gz pos 10.776632  9.681747            99              0
## 106.CEL.gz pos 10.505124  9.436763            40              1
## 107.CEL.gz neg 10.377741  8.695576            94              0

# 使用geom_beeswarm函数绘制蜂群图
ggplot(breast,aes(x=ER,y=time_survival))+
  geom_beeswarm(aes(color=factor(event_survival)),cex=1.5,size=2)+
  theme_bw()+
  labs(x="",y="Follow-up time (months)") + 
  scale_color_manual(values=c("black","red"),name="Censored",labels=c("Yes","No")) +
  scale_x_discrete(labels=c("ER neg","ER pos"))
image.png
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936 
## [2] LC_CTYPE=Chinese (Simplified)_China.936   
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C                              
## [5] LC_TIME=Chinese (Simplified)_China.936    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggbeeswarm_0.6.0 ggplot2_3.2.0    beeswarm_0.2.3  
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.5       knitr_1.23       magrittr_1.5     tidyselect_0.2.5
##  [5] munsell_0.5.0    colorspace_1.4-1 R6_2.4.0         rlang_0.4.7     
##  [9] vipor_0.4.5      stringr_1.4.0    dplyr_0.8.3      tools_3.6.0     
## [13] grid_3.6.0       gtable_0.3.0     xfun_0.8         withr_2.1.2     
## [17] htmltools_0.3.6  assertthat_0.2.1 yaml_2.2.0       lazyeval_0.2.2  
## [21] digest_0.6.20    tibble_2.1.3     crayon_1.3.4     purrr_0.3.2     
## [25] glue_1.3.1       evaluate_0.14    rmarkdown_1.13   labeling_0.3    
## [29] stringi_1.4.3    compiler_3.6.0   pillar_1.4.2     scales_1.0.0    
## [33] pkgconfig_2.0.2
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343