在R中用食物组绘制丰度

时间:2016-07-21 08:16:19

标签: r dataframe plot reshape

我有一个带

的数据框
1 col = sample ID 
2 col = bacteria phyla (out of 7 groups)
3 col = bacteria abundance
4 col = fiber category (out of normal, high, low.) 

我想用不同的门组作为填充,光纤类别作为x轴(因此3条高,低和正常)与y轴上的相对丰度相对应。

Sample  Phyla           Abundance   FiberCategory
42226   Fusobacteria    0.000007990 Normal
42226   Verrucomicrobia 0.000003990 Normal
42226   Other           0.003485600 Normal
42226   Bacteroidetes   0.403487198 Normal
42226   Firmicutes      0.577910    Normal
42226   Proteobacteria    0.0010    Normal
42226   Actinobacteria     0.0140   Normal
311101  Fusobacteria        0.00    Normal
311101  Verrucomicrobia    0.003    Normal
311101  Other             0.0009    Normal
311101  Proteobacteria  0.000743013 Normal
311101  Bacteroidetes   0.338208944 Normal
311101  Actinobacteria  0.004665200 Normal
311101  Firmicutes         0.654    Normal
4921103 Other        0.000191938    Low
4921103 Fusobacteria    0.000000000 Low
4921103 Actinobacteria  0.000246777 Low
4921103 Proteobacteria  0.007608959 Low
4921103 Verrucomicrobia 0.000004570 Low
4921103 Firmicutes    0.267081313   Low
4921103 Bacteroidetes   0.724866443 Low

我已经尝试了ggplot(vg, aes(x = VegetablesCategory, y = Abundance, fill = Phyla)),但它说情节已经损坏了。

有人可以建议如何解决此问题吗?

1 个答案:

答案 0 :(得分:0)

在您的代码中,您没有向绘图添加任何图层。您也可能希望通过 FiberCategory Phyla 汇总数据:

library(ggplot2)
library(dplyr)

vg %>% group_by(FiberCategory, Phyla) %>% summarise(Abundance = sum(Abundance)) %>%
ggplot(aes(x = FiberCategory, y = Abundance, fill = Phyla)) + geom_bar(stat = "identity")

结果:

enter image description here