导语
GUIDE ╲
小提琴图是帮助研究人员可视化数据的强大工具,尤其是在分析的质量检查和探索部分,小提琴图比箱线图更灵活地绘制变化。
背景介绍
小提琴图是箱线图和密度图的组合。今天小编给大家带来的R包vioplot,允许我们使用简单的代码对小提琴图进行个性化的绘制。
R包安装
代码语言:javascript复制BiocManager::install("vioplot")
library(vioplot)
可视化介绍
01
小提琴图和箱线图的比较
首先用同样的数据集绘制两种图形
代码语言:javascript复制data(iris)
boxplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"))
vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"))
我们可以看到小提琴图能够明显的展示数据的分布情况,这是箱线图所不具备的。
02
参数设置
我们可以看到默认的颜色可能比较单调,所以我们可以自行定义绘图颜色。
代码语言:javascript复制vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", col="lightblue")
对于分组数据:
代码语言:javascript复制vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", col=c("lightgreen", "lightblue", "palevioletred"))
legend("topleft", legend=c("setosa", "versicolor", "virginica"), fill=c("lightgreen", "lightblue", "palevioletred"), cex = 0.5)
也可以使用 col 和 border 参数分别为小提琴填充和边框自定义颜色:
代码语言:javascript复制vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", col="lightblue", border="royalblue")
矢量化
这些颜色和形状设置也可以为每个小提琴单独定制:
代码语言:javascript复制vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main="Sepal Length (Equal Area)", areaEqual = T, col=c("lightgreen", "lightblue", "palevioletred"), border=c("darkolivegreen4", "royalblue4", "violetred4"), rectCol=c("forestgreen", "blue", "palevioletred3"), lineCol=c("darkolivegreen", "royalblue", "violetred4"), colMed=c("green", "cyan", "magenta"), pchMed=c(15, 17, 19))
03
可以用于比较的split violin plot
代码语言:javascript复制iris_large <- iris[iris$Sepal.Width > mean(iris$Sepal.Width), ]
iris_small <- iris[iris$Sepal.Width <= mean(iris$Sepal.Width), ]
vioplot(Sepal.Length~Species, data=iris_large, col = "palevioletred", plotCentre = "line", side = "right")
vioplot(Sepal.Length~Species, data=iris_small, col = "lightblue", plotCentre = "line", side = "left", add = T)
title(xlab = "Species", ylab = "Sepal Length")
legend("topleft", fill = c("lightblue", "palevioletred"), legend = c("small", "large"), title = "Sepal Width")
小编总结
小提琴图在我们统计数据时是非常常用的类型,既然有这么方便的R包可以画,大家快快用起来吧!