小提琴图是【箱线图】与【核密度图】的结合,箱线图展示了分位数的位置,核密度图则展示了任意位置的密度,通过小提琴图可以知道哪些位置的数据点聚集的较多,因其形似小提琴而得名。如下图所示,其外围的曲线宽度代表数据点分布的密度,中间的箱线图则和普通箱线图表征的意义是一样的,代表着中位数、上下分位数、极差等。
最近看单细胞的文章比较多,所以常见小提琴图来展示marker基因。
今天,我们就来学习小提琴图的绘图技巧。
library(ggplot2)
library(ggsignif)
library(patchwork)
用一个简单的测试数据,包含两列:Group包含7个值,剩下一列是Values。
data <- read.table("data1.txt",sep="\t",header=T)
ggplot(data, aes(x = Group, y = Values, fill = Group)) +
geom_violin(position = position_dodge(width = 1), scale = 'width') +
scale_color_manual(values = c("#d1d2d2","#fbd3b9","#a3c6a3","#ccdecc","#c3dbed","#a1c9e5","#417bb9"))+
theme_bw()+
theme(panel.grid = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none")
通过geom_violin就实现了一个比较简单的小提琴图。
ggplot(data, aes(x = Group, y = Values, fill = Group)) +
geom_violin(position = position_dodge(width = 1), scale = 'width') +
geom_jitter(size=0.3) +
scale_color_manual(values = c("#d1d2d2","#fbd3b9","#a3c6a3","#ccdecc","#c3dbed","#a1c9e5","#417bb9"))+
theme_bw()+
theme(panel.grid = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none")
加入抖动点的效果。
ggplot(data, aes(x = Group, y = Values, fill = Group)) +
geom_violin(position = position_dodge(width = 1), scale = 'width') +
#geom_jitter(size=0.3) +
geom_boxplot(position = position_dodge(width = 1), outlier.size = 0.7, width = 0.2, show.legend = FALSE) +
scale_color_manual(values = c("#d1d2d2","#fbd3b9","#a3c6a3","#ccdecc","#c3dbed","#a1c9e5","#417bb9"))+
theme_bw()+
theme(panel.grid = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none")
也可以加入箱图一起。
也可以像前面柱状图一样,加入统计检验。
my_comparisons <- list( c("Mut", "Mut_Gain"), c("Mut", "Mut_LOH"), c("Mut_LOH", "Mut_Gain") )
ggplot(data, aes(x = Group, y = Values, fill = Group)) +
geom_violin(position = position_dodge(width = 1), scale = 'width') +
#geom_jitter(size=0.3) +
geom_boxplot(position = position_dodge(width = 1), outlier.size = 0.7, width = 0.2, show.legend = FALSE) +
scale_color_manual(values = c("#d1d2d2","#fbd3b9","#a3c6a3","#ccdecc","#c3dbed","#a1c9e5","#417bb9"))+
theme_bw()+
theme(panel.grid = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none")+
stat_compare_means(comparisons = my_comparisons)