0

我正在尝试使用 phyloseq 对象(包含 OTU、TAX 和元数据)制作多个堆叠的条形图,其中条形图上的分类群顺序保持不变,但在每个图表上,特定分类群是彩色的,而其余的是灰色的。我使用简单的数据框编写了粗略的代码草稿,但我不确定如何将其应用于 phyloseq 对象。

这是我想出的代码:

 ##good_phylum being the specfic taxa i want colorized
 good_phylum <- c("p__Firmicutes")
 ## pig_df contains taxa information and relative abundance
 pig_df[!(pig_df$Phylum %in% good_phylum),]$Phylum <- "OTHER"
 ##organizing color palette so Firmicutes is colored blue, the rest are grey
 colors <- rep("#0000", length(unique(pig_df$Phylum)))
 colors[match('p__Firmicutes', sort(unique(pig_df$Phylum)))] <- 'blue'

 ##Plot stacked bar plot
 ggplot2(pig_df) + geom_bar(position="stack", 
 stat="Phylum") + scale_fill_manual(values = colors) + 
 theme(legend.text = element_text(size = 14))

样本数据:

structure(list(Phylum = c("p__Actinobacteria", "p__Bacteroidetes", 
"p__Firmicutes", "p__Lentisphaerae", "p__Proteobacteria", "p__Spirochaetes"
), AL.5 = c(5.54534, 44.05287, 37.01594, 0, 2.71389, 10.67196
), AL.4 = c(0, 40.61791, 29.41689, 0, 0.39118, 29.57402), AL.3 = c(0.51442, 
39.55305, 35.79623, 0, 2.79092, 21.34539), AL.2 = c(2.97639, 
53.8286, 25.08614, 0, 8.70794, 9.40094), AL.1 = c(3.35874, 39.20605, 
25.74435, 0.30047, 9.23324, 22.15715), LF.5 = c(0.40971, 3.95695, 
17.63298, 0.06856, 76.56688, 1.36492), LF.4 = c(2.7231, 12.9073, 
81.70363, 0.09188, 1.12626, 1.44782), LF.3 = c(9.58431, 7.14942, 
56.77299, 0, 20.15845, 6.33483), LF.2 = c(0.26317, 17.63049, 
27.01207, 0, 53.73687, 1.3574), LF.1 = c(5.48864, 36.46061, 41.32865, 
0, 1.71807, 15.00404)), class = "data.frame", row.names = c(NA, 
-6L))

就像我说的那样,我已经写了上面的代码来处理一个简单的数据框,但我想把它用到我的 phyloseq 工作流程中。

提前致谢!

*编辑:使用最小的示例数据框进行了更新,并更新了代码以与所述 df 一起使用

4

1 回答 1

1

不是 100% 关于您想要的结果,但据我所知,一种选择是:

  1. 使用命名的颜色向量。
  2. 为了让你的酒吧着色,你必须在fill美学上进行映射。
  3. 正如您提到的要制作多个图表,我建议使用绘图功能:

注意:我放弃了stat="Phylum",因为我无法弄清楚它来自哪个包裹stat

library(ggplot2)


plot_phylum <- function(pig_df, good_phylum) {
  colors <- c("grey", "blue")
  names(colors) < c("FALSE", "TRUE")
  labels <- c("OTHER", good_phylum)
  names(labels) < c("FALSE", "TRUE")
  ##Plot stacked bar plot
  ggplot(pig_df, aes(x = factor(1), fill = Phylum %in% good_phylum)) + 
    geom_bar(position="stack") + 
    scale_fill_manual(values = colors, labels = labels, name = NULL) + 
    theme(legend.text = element_text(size = 14))
}
plot_phylum(pig_df, "p__Firmicutes")

plot_phylum(pig_df, "p__Bacteroidetes")

于 2021-09-22T18:34:14.157 回答