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

2021-12-06 18:17:20 浏览数 (1)

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

  • 首选应得到data如下列所示:

然后循环作图:

代码语言:javascript复制
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() 
  • 将宽数据data转换成长数据data1
代码语言:javascript复制
#=======================================================
#一张图
#=======================================================

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()

0 人点赞