关于使用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", ) 

这样唯一的缺点是旁边的图例会出现问题,手动删掉即可。 

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值