好看又可爱的散点图——Beeswarm蜂群图

2022-03-28 15:33:08 浏览数 (1)

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

一、beeswarn包

代码语言:javascript复制
install.packages("beeswarm")
library(beeswarm)

1. beeswarm

代码语言:javascript复制
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)

代码语言:javascript复制
 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 图例中点的形状
代码语言:javascript复制
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. 绘制点的四种方法比较

代码语言:javascript复制
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")  #图1
beeswarm(distro, col = 2:3, pch = 16,method = "swarm",
         main = "method = swarm")  #图2
beeswarm(distro, col = 2:3, pch = 16,method = "center",
         main = "method = center")  #图3
beeswarm(distro, col = 2:3, pch = 16,method = "hex",
         main = "method = hex")  #图4

4. 五种corral方法比较

代码语言:javascript复制
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"结合起来

代码语言:javascript复制
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"

代码语言:javascript复制
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()

代码语言:javascript复制
install.packages("ggbeeswarm")
library(ggbeeswarm)
library(ggplot2)

1.geom_quasirandom()

(1)

代码语言:javascript复制
ggplot(iris,aes(Species, Sepal.Length))   geom_jitter()  
#geom_jitter() 函数是用来消除点的重合的一种方法
ggplot(iris,aes(Species, Sepal.Length))   geom_quasirandom()   #geom_quasirandom默认值
代码语言:javascript复制
ggplot(mpg,aes(class, hwy))   geom_quasirandom()  #默认值
代码语言:javascript复制
ggplot(mpg,aes(hwy, class))   geom_quasirandom(groupOnX=FALSE)  #坐标轴转换
代码语言:javascript复制
ggplot(mpg,aes(class, hwy))   geom_quasirandom(varwidth = TRUE)
#有的组只有几个点,用varwidth = TRUE调整宽度
代码语言:javascript复制
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)几种散点的分布方式

代码语言:javascript复制
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()

代码语言:javascript复制
ggplot(iris,aes(Species, Sepal.Length))   geom_beeswarm()  #默认
代码语言:javascript复制
ggplot(mpg,aes(hwy, class))   geom_beeswarm(size=0.5,groupOnX=FALSE)
#size改变点大小,groupOnX 坐标轴转换
代码语言:javascript复制
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 表示扩展坐标轴显示范围
代码语言:javascript复制
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)

小编总结:

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

0 人点赞