蜜蜂图(beeswarm)是类似于“抖动图”的一维散点图,同时每一个重叠的点是分开可见的,可能多簇形如蜜蜂群所以叫蜜蜂图。绘制蜜蜂图可以用R包beeswarm
或者基于ggplot2的ggbeeswarm
。
常规的点图和蜜蜂图是有很明显的区别,总体来说蜜蜂图能呈现更多的细节信息:
# 安装包
# install.packages("beeswarm")
library("beeswarm")
stripchart(
decrease ~ treatment,
data = OrchardSprays,
vertical = TRUE,
log = "y",
method = 'jitter',
jitter = 0.2,
cex = 1,
pch = 16,
col = rainbow(8),
main = '点图'
)
beeswarm(
decrease ~ treatment,
data = OrchardSprays,
log = TRUE,
pch = 16,
col = rainbow(8),
main = '蜜蜂图'
)
下面用一个真实的乳腺癌的案例进行绘图展示:
# 安装包
library("beeswarm")
# 加载乳腺癌演示数据
data(breast)
# 绘制蜜蜂图
beeswarm(
# 生存时间和(ER)雌激素受体
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",
border = NULL,
box.col = "black",
# 去掉边框
bty = "n",
pch = 16,
col = 1:2
)
绘制相对简单,如果感兴趣可以阅读下参考资料提供的文章。
参考资料:
1.http://www.cbs.dtu.dk/~eklund/beeswarm/
2.https://flowingdata.com/2016/09/08/beeswarm-plot-in-r-to-show-distributions/