【发布时间】:2021-02-09 21:39:15
【问题描述】:
TL;DR:我想使用基因列表的标准化计数图生成降价。由于这个列表很长(> 100 个基因),我想在一个图形页面上为前 16 个基因生成一个 4x4 图形的“网格”,然后对基因 17 到 32 等生成一个“网格”……直到最后的名单。目前,我的代码只显示 16 个基因,即使我在循环内运行 grid.arrange 命令(当我在循环内使用“绘图”函数时它工作但它每页只显示一个图形 c)。
我目前正在进行一些 RNAseq 分析,并且正在寻找两个群体之间的差异表达基因 (DEG)。为了更直观地表示 DEG,我想为每个群体绘制一些感兴趣基因 (GoI) 的归一化计数。
但是,这些 GOI 的列表可能很长(例如,如果我专注于编码膜蛋白的 DEG,我有大约 159 个候选者)。我可以使用 for 循环绘制它们,使用以下代码(来自第一次分析):
# top gene contains all of Gene of Interest
# group_origin contains the factor used to discriminate cell population to compare
# dds_g is a dds object from DESeq2 and contained counts number I want to plot
for (i in unique(1:length(top_gene))){
gene <- top_gene[i]
d <- plotCounts(dds_g, gene = gene, intgroup = "group_origin", returnData = TRUE)
b <- ggplot(d, aes(x = group_origin, y = count)) +
stat_boxplot(geom = 'errorbar', aes(colour = factor(group_origin)), width = 0.2) +
geom_boxplot(aes(colour = factor(group_origin)), width = 0.2) +
stat_summary(fun.y=mean, geom="point", shape=17, size=1.5, aes(color=factor(group_origin), fill=factor(group_origin))) +
labs (title = paste0(resg_cb_db$symbol[gene],' (',gene,')'), x = element_blank()) +
theme_bw() +
scale_color_manual(values = mycolors) +
theme(text= element_text(size=10),
axis.text.x = element_text(size = 7.5, angle = 45, hjust = 1),
axis.text.y = element_text(size = 10),
legend.position = "none")
plot(b)
}
通过使用这种方法,我能够(单独)为我的“top_gene”向量中包含的所有基因 ID 生成图。
但是,降价会在每页创建一个图,当您拥有大量基因时,这很烦人。我想对它们进行分组,例如,列表中的每个基因 1 到 16 都被绘制在一页上,然后是它的 16 到 32,等等......
我已经尝试过(没有成功)par mfrow。我还使用以下代码(来自另一个分析示例)尝试了 Grid.extra:
for (i in unique(1:length(top_gene))){
gene <- top_gene[i]
d <- plotCounts(dds, gene = gene, intgroup = "cell_type", returnData = TRUE)
b <- ggplot(d, aes(x = cell_type, y = count)) +
stat_boxplot(geom = 'errorbar', aes(colour = factor(cell_type)), width = 0.2) +
geom_boxplot(aes(colour = factor(cell_type)), width = 0.2) +
stat_summary(fun.y=mean, geom="point", shape=17, size=1.5, aes(color=factor(cell_type), fill=factor(cell_type))) +
labs (title = paste0(resg$symbol[gene],' (',gene,')'), x = element_blank()) +
theme_bw() +
scale_color_manual(values = mycolors) +
theme(text= element_text(size=7),
axis.text.x = element_text(size = 7, angle = 45, hjust = 1),
axis.text.y = element_text(size = 6),
legend.position = "none")
plot_list[[gene]] <- b
}
t <- length(plot_list)
x <- seq(1, t, by = 15)
for (i in x){
z <- i+1
if (!is.na(x[z])) {
test_list <- plot_list[c(x[i]:x[z])]
do.call(grid.arrange,test_list)
}
else {
z <- length(plot_list)
test_list <- plot_list[c(x[i]:z)]
do.call(grid.arrange,test_list)
}
}
这给了我一个错误“x[i]:z 中的错误:参数 NA / NaN”
这里的想法是,对于每 16 个基因,绘制一个 4x4 图形页面。当然,当它到达最后的“16 组”时,剩下的基因少于 16 个(除非你有可以除以 16 的基因总数)。所以这就是为什么 if 循环可以防止错误(但它不起作用)。
另外,我尝试删除最后一部分,并尝试生成列表中的第一个“9 x 16 基因”,丢弃最后一个。它“有效”是因为我可以清楚地看到前 16 个基因以 4 x 4 的形式绘制,但其余的则一无所获。
为什么使用“plot(b)”绘制inside for 循环是有效的,但在 for 循环内创建的 list() 上也不能使用 grid.arrange?
非常抱歉我的代码,我知道它并不完美(我仍在学习所有这些)但我希望它足够清楚,让您理解我的问题......
谢谢!
!编辑! : 通过添加 (i in length(x)) 解决了第一个错误。觉得自己很蠢 :D。无论如何,它仍然只是“打印”了 16 个地块而不是 159 个......
【问题讨论】: