Beeswarm图(列散点图或小提琴散点图)绘制方法

今天给大家介绍一个好看又简单的散点图展示方法,叫做Beeswarm图(也称为列散点图或小提琴散点图),是一种绘制会重叠的点的方法,使它们从重叠变成彼此相邻。 除了减少过度绘图之外,它还有助于可视化每个点(类似于小提琴图)上的数据密度,同时仍单独显示每个数据点。

一、beeswarn包

install.packages("beeswarm")library(beeswarm)

1. 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')

 

可以看到在第一个图中,有很多散点是重复的,而在Beeswarm图中,将这些重复的点分散开,变成邻近的点。

 

2. 定义点的不同颜色

data(breast)

 beeswarm(time_survival ~ ER, data = breast,  #breast数据,根据ER分组,用time_survival 值绘制散点图pch = 16, pwcol = 1 + as.numeric(event_survival),  #pch点的形状,pwcol 根据event_survival分组颜色#使用pwpch,pwcol和pwbg来控制每个单独数据点的“逐点”特性,这里可以给每个点赋值颜色参数    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)  # pch 图例中点的形状

myCol <- lapply(distributions, function(x) cut(x, breaks = quantile(x), labels = FALSE))#将distributions的数据划分为四个范围,定义成1-4的数值,然后用这个数据定义颜色beeswarm(distributions, pch = 16, pwcol = myCol)legend("bottomright", legend = 1:4, pch = 16, col = 1:4, title = "Quartile")

3. 绘制点的四种方法比较

set.seed(123)  distro <- list(runif = runif(100, min = -3, max = 3),               rnorm = rnorm(100))  ##随机生成数据beeswarm(distro, col = 2:3, pch = 16,method = "square",         main = "method = square")  #图1beeswarm(distro, col = 2:3, pch = 16,method = "swarm",         main = "method = swarm")  #图2beeswarm(distro, col = 2:3, pch = 16,method = "center",         main = "method = center")  #图3beeswarm(distro, col = 2:3, pch = 16,method = "hex",         main = "method = hex")  #图4

4. 五种corral方法比较

beeswarm(distributions,         pch = 21, col = c(1,7,5), bg = "#8B2323",           # col圆点边的颜色,bg圆点填充颜色         corral = "gomit",             # 图二gutter,图三wrap,图四random,图五gomit,         main = "corral = gomit")

 

 

5. 将beeswarms与 “boxplot” “bxplot”结合起来

boxplot(len ~ dose, data = ToothGrowth,    outline = FALSE,        main = 'boxplot + beeswarm')  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)

 

 

 

6.  “side” and “priority”

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"')

 

二、ggbeeswarm包绘制蜂群图

ggbeeswarm提供了两种使用ggplot2创建蜂群图的方法。 一个是geom_quasirandom(),另一个是geom_beeswarm()

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

1.geom_quasirandom()

(1)

ggplot(iris,aes(Species, Sepal.Length)) + geom_jitter()  #geom_jitter() 函数是用来消除点的重合的一种方法ggplot(iris,aes(Species, Sepal.Length)) + geom_quasirandom()   #geom_quasirandom默认值

ggplot(mpg,aes(class, hwy)) + geom_quasirandom()  #默认值

 

 

ggplot(mpg,aes(hwy, class)) + geom_quasirandom(groupOnX=FALSE)  #坐标轴转换

 

ggplot(mpg,aes(class, hwy)) + geom_quasirandom(varwidth = TRUE)#有的组只有几个点,用varwidth = TRUE调整宽度

 

sub_mpg <- mpg[mpg$class %in% c("midsize", "pickup", "suv"),]ggplot(sub_mpg, aes(class, displ, color=factor(cyl))) + geom_quasirandom(dodge.width=1)#闪避,dodge.width 调整组内的不同颜色亚组的距离

 

 

(2)几种散点的分布方式

ggplot(iris, aes(Species, Sepal.Length)) + geom_quasirandom(method = "tukey") +ggtitle("Tukey texture")   #图1,图基ggplot(iris, aes(Species, Sepal.Length)) + geom_quasirandom(method = "tukeyDense") +ggtitle("Tukey + density")  #图2,图基+密度ggplot(iris, aes(Species, Sepal.Length)) + geom_quasirandom(method = "frowney") +ggtitle("Banded frowns")  #图3,皱眉型(看起来不高兴的样子)ggplot(iris, aes(Species, Sepal.Length)) + geom_quasirandom(method = "smiley") +ggtitle("Banded smiles")  #图4,微笑型ggplot(iris, aes(Species, Sepal.Length)) + geom_quasirandom(method = "pseudorandom") +ggtitle("Jittered density")  #图5,伪随机ggplot(iris, aes(Species, Sepal.Length)) + geom_beeswarm() + ggtitle("Beeswarm")  #图6,蜂群图

 

2. geom_beeswarm()

ggplot(iris,aes(Species, Sepal.Length)) + geom_beeswarm()  #默认

 

ggplot(mpg,aes(hwy, class)) + geom_beeswarm(size=0.5,groupOnX=FALSE)#size改变点大小,groupOnX 坐标轴转换

 

ggplot(mpg,aes(hwy, class)) + geom_beeswarm(size=.5,groupOnX=FALSE) + scale_y_discrete(expand=expand_scale(add=c(0.5,1))) #scale_y_discrete是对y轴离散变量进行处理,expand 表示扩展坐标轴显示范围

 

sub_mpg <- mpg[mpg$class %in% c("midsize", "pickup", "suv"),]ggplot(sub_mpg, aes(class, displ, color=factor(cyl))) + geom_beeswarm(dodge.width=0.5)

 

小编总结:

虽然方法很简单,但是只要我们根据自己的数据仔细调整颜色和背景等,就可以画出好看又高级的展示图呢~

统计与绘图

绘制序列标识图-gglogo

2020-8-28 1:01:46

统计与绘图

可以旋转的3D韦恩图

2020-8-28 1:15:03

声明 本网站部分文章源于互联网,出于传递更多信息和学习之目的转载,并不保证内容正确或赞同其观点。
如转载稿涉及失效、版权等问题,请立即联系管理员;我们会予以修改、删除相关文章,请留言反馈
Notice: When your legal rights are being violated, please send an email to: [email protected].
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索