[ggplot2]小提琴图经验总结

小提琴图 (Violin Plot)是用来展示多组数据的分布状态以及概率密度,是优于箱线图的一种统计图形。他结合了箱线图与密度图,箱线图位于小提琴图内部,两侧是数据的密度图,能显示出数据的多个细节,而学会软件来绘制精美的小提琴图,则是科研中必备的手段。

一、前期准备
1. 所用软件与版本:R 4.0.5
2. 所需R包:ggplot2 , cowplot, ggpub, magrittr
3. 示例数据:iris数据集
二、绘制一张粗糙的violin plot
library(ggplot2)
p=iris %>% ggplot(aes(x=Species,y=Sepal.Length,fill=Species))+geom_violin()
p
violin plot1

这是针对iris数据集的Sepal.Length特征绘制的小提琴图,大家调用ggolot函数就可以直接画出来,其中x通常是因子列,y是要画的特征,而fill是按照不同的类别分类填充,比如在这里fill=Species,说明按照Species(因子列,如果不是要提前用as.factor函数进行转换),三个不同花的类别还选择不同的颜色,这里没有指定各自的颜色,ggolot默认是图中的颜色。这时就有读者问了,这哪里体现了箱线图?别急,我们马上添上去!

三、修图
1. 去除后面背景,这里采用ggplot2中的bw主题
p=p+theme_bw()
2. 只显示x轴和y轴,不显示图例
  p=p+theme(legend.position="none",
        axis.text.x = element_text(hjust = 0.5, vjust = 0.5),
        axis.text.y = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_text( size=rel(1)),
        panel.border = element_blank(),
        axis.line = element_line(colour = "black",size=1)
  )
3. 添加箱线图
p=p+geom_boxplot(width=0.2,position=position_dodge(0.9),outlier.colour = NA,fill="white")
4. 改变默认颜色
color = c("#33B44A","#EE3536","#3A429B")
p=p+scale_fill_manual(values = color)

此时我们看下效果


violin plot2

这里给出完整代码:

library(ggplot2)
color = c("#33B44A","#EE3536","#3A429B")
iris %>% ggplot(aes(x=Species,y=Sepal.Length,fill=Species))+geom_violin()+
  geom_boxplot(width=0.2,position=position_dodge(0.9),outlier.colour = NA,fill="white")+
  theme_bw()+
  theme(legend.position="none",
        axis.text.x = element_text(hjust = 0.5, vjust = 0.5),
        axis.text.y = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_text( size=rel(1)),
        panel.border = element_blank(),
        axis.line = element_line(colour = "black",size=1)
  )+scale_fill_manual(values = color)
四、添加不同类别间的pvalue

我们还想让这张图上显示更多的信息:

● 添加比较两组或多组的均值差异的pvalue
● 添加多组样本的组间比较的pvalue

在这里我们运用Kruskal-Wallis检验和wilcox检验,具体原理,具体原理本文不做阐述,读者请移步非参数统计的相关内容。我们给出代码,这里会用到ggpubr包。

library(ggplot2)
library(ggpubr)
color = c("#33B44A","#EE3536","#3A429B")
my_comparisons <- list( c("setosa", "versicolor"), c("versicolor", "virginica"), c("setosa", "virginica") )
iris %>% ggplot(aes(x=Species,y=Sepal.Length,fill=Species))+geom_violin()+
  geom_boxplot(width=0.2,position=position_dodge(0.9),outlier.colour = NA,fill="white")+
  theme_bw()+
  theme(legend.position="none",
        axis.text.x = element_text(hjust = 0.5, vjust = 0.5),
        axis.text.y = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_text( size=rel(1)),
        panel.border = element_blank(),
        axis.line = element_line(colour = "black",size=1)
  )+scale_fill_manual(values = color)+
  stat_compare_means( comparisons = my_comparisons)+
  stat_compare_means(label.y = 10)
violinplot3

我们还可以在stat_compare_means函数里面设置不同的参数,这里默认使用wilcox检验,最后一个stat_compare_means函数还可以使用方差分析(method=""anova")。
关于为ggplot图形添加pvalue更多的细节,可以参考这位博主写的文章:

数据可视化——R语言为ggplot图形添加P值和显著性水平_zhouhucheng00的博客-CSDN博客

五、多个小提琴图组合

当我们想要画多个特征,即多个小提琴图组合成一张大图,比如iris数据集中一共有四个特征,我们要将他们全部按照上面的画法贴在画布上,通常我们用for循环处理,或者ggplot中有函数为facet_grid(),这里不做介绍(个人觉得在实际论文撰写中,用facet_grid组合的图形不是很好看)下面介绍我的处理方法,这里要用到cowplot包。

library(ggplot2)
library(ggpubr)
library(cowplot)
color = c("#33B44A","#EE3536","#3A429B")
my_comparisons <- list( c("setosa", "versicolor"), c("versicolor", "virginica"), c("setosa", "virginica") )
p=list()
sites = apply(iris[,-5], 2, function(x) max(x)+2.5)
for (i in colnames(iris)[1:4]) {
p[[i]]=iris %>% ggplot(aes_string(x="Species",y=i,fill="Species"))+geom_violin()+
  geom_boxplot(width=0.1,position=position_dodge(0.9),outlier.colour = NA,fill="white")+
  theme_bw()+
  theme(legend.position="none",
        axis.text.x = element_text(hjust = 0.5, vjust = 0.5),
        axis.text.y = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_text( size=rel(1)),
        panel.border = element_blank(),
        axis.line = element_line(colour = "black",size=1)
  )+scale_fill_manual(values = color)+
  stat_compare_means( comparisons = my_comparisons)+
  stat_compare_means(label.y = as.numeric(sites[i]))
}
plot_grid(plotlist = p,align = "h",labels =  LETTERS[1:4])
ggsave("violin plots.pdf",height = 10,width = 10)
Last

在这张图的绘制中,由于用到了for循环,我将aes改成了aes_string使得里面能以字符串的形式输入,然后考虑到不同小提琴图KW检验的pvalue位置要不同,所以在高度设置上我们取不同特征的最大值+2.5,并且要使箱线图都能在小提琴图内部,我们设置宽度为0.1,使得能兼顾到每一个小提琴图。此外,最后的plot_grid函数,我们制定align="h",表示按照行来组合图形,并添加标号,当然也可以按列组合,具体可以查看plot_grid函数的相关参数。

以上就是我对于画小提琴图的相关总结。

你可能感兴趣的:([ggplot2]小提琴图经验总结)