关于使用ggplot2作多组双条件的小提琴图+箱线图
0.使用的R包
library("readxl")
library("dplyr")
library("ggplot2")
library("ggthemes")
1.处理的数据
我们这里处理的数据是两到三列的数据框,每个组(基因)需要有两个condition(多个也行)对应。我们这里画的图是polyA的长度。
2.使用ggplot2作图
p <- ggplot(total_gene_data, aes(gene, lena, fill = condition)) + ###aes后面是x,y轴的值
geom_violin(aes(fill = condition),width = 0.7, scale = "width", size = 0.8)###width参数设为0.7,
加上箱线图
p <- ggplot(total_gene_data, aes(gene, lena, fill = condition)) +
geom_violin(aes(fill = condition),width = 0.7, scale = "width", size = 0.8) +
geom_boxplot(width = 0.2, position = position_dodge(0.7), ###position调整会对不齐
outlier.shape = NA, cex = 0.8) ###NA这里不画出离群值
自定义颜色 —— —— 使用 scale_fill_manual
p <- ggplot(total_gene_data, aes(gene, lena, fill = condition)) +
geom_violin(aes(fill = condition),width = 0.7, scale = "width", size = 0.8) +
geom_boxplot(width = 0.2, position = position_dodge(0.7),
outlier.shape = NA, cex = 0.8) +
scale_fill_manual(values = c("#0255E8", "#E81D22"))
添加显著性***号
p <- p + stat_compare_means(aes(condition = condition), #按分组进行统计检验
method = "t.test", #非配对t检验
symnum.args = list(cutpoint=c(0,0.0005,0.005,0.05,1),
symbols=c("***","**","*", NA)),
label = "p.signif",
label.y = location$lena + 4, #添加显著性符号的位置
size= 8)
加上背景
p <- p + theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.text.x = element_text(angle=60,size=16,vjust = 1,hjust =1,family = "Bangers", color = "black", face = "bold"),
axis.text.y = element_text(size =16, face = "bold", family = "Bangers", color = "black"),
axis.title.y = element_text(size = 20, color = "black", face = "bold.italic"),
legend.text = element_text(size = 15, family = "Playfair"),
legend.title = element_blank())
这个时候图差不多花完了,但是效果可能不太好,因为箱线图和小提琴图是一样的颜色,如果两者都需要自定义颜色,实现起来难度很大。因为ggplot2里面自定义两者的颜色是同一个函数。本人仅成功过将箱线图改成白色。
p <- ggplot(total_gene_data, aes(gene, lena, fill = condition)) +
geom_violin(aes(fill = condition),width = 0.7, scale = "width", size = 0.8) +
geom_boxplot(aes(fill = "white", colour = condition), width = 0.2, position = position_dodge(0.7),
outlier.shape = NA, cex = 0.8) +
scale_fill_manual(values = c("#0255E8", "#E81D22","white")) +
scale_colour_manual(values = c("black", "black"))+
theme(legend.title = element_blank())+
theme_classic(base_size = 20)+
##geom_jitter(aes(fill = condition), shape = 21, position = position_jitterdodge(dodge.width = 0.9))+
labs(title = NULL, x = NULL, y = "Poly(A) length", )
这样唯一的缺点是旁边的图例会出现问题,手动删掉即可。