美文网首页
1. ggpubr画vilinplot

1. ggpubr画vilinplot

作者: 吹不散的烽烟 | 来源:发表于2023-12-27 13:56 被阅读0次

画单图

expr$Sam <- factor(expr$Sam,levels = c("Young","Aged"))
p <- ggviolin(subset(expr,Id3 >0), x = "Sam", # 分组列
              y = "Id3", # 基因列
              fill = "Sam", #按分组填充颜色
              alpha = 1,#透明图 0-1
              width = 0.5, #宽度
              legend = "top",legend.title = "Group",#legend及位置
              font.legend = c(12, "bold", "black"),
              # ylim=c(0, 4), #y轴长度
              ylab="Expression levels", xlab=FALSE, #xy轴标签,去掉X轴标签
              font.y = 16,#xy轴标题大小
              x.text.angle = 45, y.text.angle = 90,#xy轴标题角度
              font.tickslab = c(15,"plain","black"), #xy轴刻度大小/样式/颜色
              add = "boxplot", #添加图 "dotplot", "jitter", "boxplot", "point"
              add.params = list(fill = "white", #填充颜色 白色
                                width = 0.1,#宽度
                                linetype = 1),#线型
              palette ="jco" #调色板调色如c("blue", "red") 
              # 也可以直接按杂志来改,如 "npg", "aaas", "lancet",
              # "jco", "ucscgb", "uchicago", "simpsons" and "rickandmorty".
) 
# my_comparisons <- list(c("0", "1"), c("0", "2"), c("0", "3"), c("0" ,"4"))##多分组设定比较
# my_comparisons <- list(c("0", "1"))##多分组设定比较

p <- p + stat_compare_means(method = "t.test", 
                            label = "p.signif",##星号设置
                            aes(label = "p.format"), #显示方式
                            label.x.npc ="middle", 
                            # label.y = 3.2,#位置
                            size = 5,
                            # comparisons = my_comparisons
                            ) 
p
ggsave("Id3 _Sam.pdf",width=2.3,height=4)
image.png

批量画图

#批量画图
plist<-list()
for (i in 1:length(genes)){
  expr_tmp<-expr[,c(genes[i],"Sam")]
  pb1 <- ggviolin(subset(expr_tmp,genes[i]>0), x = "Sam", # 分组列
                      y = genes[i], # 基因列
                      fill = "Sam", #按分组填充颜色
                      alpha = 1,#透明图 0-1
                      width = 0.5, #宽度
                      legend = "top",legend.title = genes[i],#legend及位置
                      font.legend = c(12, "bold", "black"),
                      # ylim=c(0, 4), #y轴长度
                      ylab="Expression levels", xlab=FALSE, #xy轴标签,去掉X轴标签
                      font.y = 16,#xy轴标题大小
                      x.text.angle = 45, y.text.angle = 90,#xy轴标题角度
                      font.tickslab = c(15,"plain","black"), #xy轴刻度大小/样式/颜色
                      add = "boxplot", #添加图 "dotplot", "jitter", "boxplot", "point"
                      add.params = list(fill = "white", #填充颜色 白色
                                        width = 0.1,#宽度
                                        linetype = 1),#线型
                      palette ="jco" #调色板调色如c("blue", "red") 
                      # 也可以直接按杂志来改,如 "npg", "aaas", "lancet",
                      # "jco", "ucscgb", "uchicago", "simpsons" and "rickandmorty".
  ) 
  # +
    stat_compare_means(method="t.test",hide.ns = F,comparisons =my_comparisons,label="p.signif")
  plist[[i]]<-pb1
} 

# cowplot
library(cowplot)
p <- plot_grid(plotlist = plist, ncol = 6)
ggsave("boxplot1208.pdf",width = 14,height = 4)

image.png

参考文献: R语言ggpubr绘制小提琴图(violin plot) - 知乎 (zhihu.com)

相关文章

网友评论

      本文标题:1. ggpubr画vilinplot

      本文链接:https://www.haomeiwen.com/subject/kibrndtx.html