生信修炼good code

循环绘图和多分组多基因(变量)差异展示

2021-11-04  本文已影响0人  柳叶刀与小鼠标

经常遇到‘一个循环绘制每一个差异基因在肿瘤和正常的表达差异’和‘需要在一张图中展示多个差异基因在肿瘤和正常组的表达分布’需求。如下列两张图所示:


然后循环作图:

list <- names(data)[2:11]
list
plot_list = list() 
for (i in 1:3) { 
  p = ggboxplot(data, x='group',  y= as.character(list[i]), 
                bxp.errorbar = T, color = 'group',
                outlier.shape = NA,  add = "point",
                palette =c("#00AFBB","#FC4E07") )+stat_compare_means()
  plot_list[[i]] = p } 
# Another option: create pdf where each page is a separate plot. 
pdf("plots.pdf") 
for (i in 1:10) {print(plot_list[[i]]) } 
dev.off() 

#=======================================================
#一张图
#=======================================================

library(reshape2)
library(tidyr)
colnames(data)
names(data)[2]
names(data)[11]
#使用data数据框中非subtype的第一个列名和最后一个列名
data1 <- gather(data, gene, count, BCL11A:ZCCHC7 )
head(data1)
p <- ggboxplot(data1, x = "gene", y = "count",size=0.5, bxp.errorbar = T, 
               color = "group",  palette =c( "#FC4E07", "#00AFBB"),
               add.params = list(size=0.5), outlier.shape = NA, 
               add = "point") + 
stat_compare_means(aes(group = group), label = "p.format")
p
pdf(file = 'expr_subtype.pdf', height = 4,width = 14)
p
dev.off()
上一篇下一篇

猜你喜欢

热点阅读