小提琴圖是【箱線圖】與【核密度圖】的結(jié)合檩奠,箱線圖展示了分位數(shù)的位置球化,核密度圖則展示了任意位置的密度杆勇,通過(guò)小提琴圖可以知道哪些位置的數(shù)據(jù)點(diǎn)聚集的較多齐鲤,因其形似小提琴而得名斥废。如下圖所示,其外圍的曲線寬度代表數(shù)據(jù)點(diǎn)分布的密度给郊,中間的箱線圖則和普通箱線圖表征的意義是一樣的牡肉,代表著中位數(shù)、上下分位數(shù)淆九、極差等统锤。
最近看單細(xì)胞的文章比較多,所以常見小提琴圖來(lái)展示marker基因炭庙。
今天饲窿,我們就來(lái)學(xué)習(xí)小提琴圖的繪圖技巧。
library(ggplot2)
library(ggsignif)
library(patchwork)
用一個(gè)簡(jiǎn)單的測(cè)試數(shù)據(jù)煤搜,包含兩列:Group包含7個(gè)值免绿,剩下一列是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")
通過(guò)geom_violin就實(shí)現(xiàn)了一個(gè)比較簡(jiǎn)單的小提琴圖擦盾。
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")
加入抖動(dòng)點(diǎn)的效果嘲驾。
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")
也可以加入箱圖一起。
也可以像前面柱狀圖一樣迹卢,加入統(tǒng)計(jì)檢驗(yàn)辽故。
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)