【问题标题】:Plotting Bacteria according to Food Groups & Abundance in R根据 R 中的食物组和丰度绘制细菌
【发布时间】:2016-10-10 04:49:17
【问题描述】:

我有一个包含四种细菌类型的数据框:R、B、P、Bi - 这是在 variable.x 中

value.y 是它们的丰度,variable.y 是它们所在的各个组。

我想根据食物类别对它们进行绘制:“FiberCategory”、“FruitCategory”、“VegetablesCategory”和“WholegrainCategory”。我已经制作了 4 个单独的文件,它们具有以下内容:

Sample   Bacteria   Abundance   Category Level
30841102    R       0.005293192 1        Low
30841102    P       0.000002570 1        Low
30841102    B       0.005813275 1        Low
30841102    Bi      0.000000000 1        Low
49812105    R       0.003298709 1        Low
49812105    P       0.000000855 1        Low
49812105    B       0.131147541 1        Low
49812105    Bi      0.000350086 1        Low

所以,我想要一个条形图,显示每个类别中每种细菌的数量。所以它应该是 4 个图,对于每个细菌,y 轴上的值和 x 轴上的食物类别。

我试过这段代码:

library(dplyr)
genus_veg %>% group_by(Genus, Abundance) %>% summarise(Abundance = sum(Abundance)) %>% 
ggplot(aes(x = Level, y= Abundance, fill = Genus)) + geom_bar(stat="identity")

但是得到这个错误:

Error: cannot modify grouping variable

有什么建议吗?

【问题讨论】:

  • 请您进行编辑,使代码中的 data.frame 列名和变量名匹配。你看过facet_wrap吗?
  • @RichardTelford 我已更改数据并将它们分成 4 组。新代码现在已编辑。请问您有什么建议吗?
  • @surreals 数据框中的哪个变量是食物类别?

标签: r plot ggplot2


【解决方案1】:

TL;DR 将单个地块与cowplot

合并

超级不清楚问题的另一种解释,这次来自:

在 R 中根据食物组和丰度绘制细菌

想根据食物类别对它们进行绘制:“FiberCategory”、“FruitCategory”、“VegetablesCategory”和“WholegrainCategory”。我制作了 4 个单独的文件

您可能要求:

  • 你想要一个条形图
  • 您需要 4 个地块,每个食品类别一个地块
  • x 轴 = 细菌类型
  • y 轴 = 细菌丰度

输入

假设您对每个食品类别都有一个数据框。 (同样,我使用的是虚拟数据)

library(tidyr)
library(dplyr)
library(ggplot2)

## The categories you have defined
bacteria <- c("R", "B", "P", "Bi")
food <- c("FiberCategory", "FruitCategory", "VegetablesCategory", "WholegrainCategory")

## Create dummy data for plotting
set.seed(1)
num_rows <- length(bacteria)
num_cols <- length(food)
dummydata <- 
  matrix(data = abs(rnorm(num_rows*num_cols, mean=0.01, sd=0.05)),
         nrow=num_rows, ncol=num_cols)
rownames(dummydata) <- bacteria
colnames(dummydata) <- food
dummydata <-
  dummydata %>%
  as.data.frame() %>%
  tibble::rownames_to_column("bacteria") %>% 
  gather(food, abundance, -bacteria)


## If we have 4 data frames
filter_food <- function(dummydata, foodcat){
  dummydata %>%
    filter(food == foodcat) %>% 
    select(-food)
}
dd_fiber <- filter_food(dummydata, "FiberCategory")
dd_fruit <- filter_food(dummydata, "FruitCategory")
dd_veg <- filter_food(dummydata, "VegetablesCategory")
dd_grain <- filter_food(dummydata, "WholegrainCategory")

一个数据框看起来像这样

#> dd_grain
#  bacteria  abundance
#1        R 0.02106203
#2        B 0.10073499
#3        P 0.06624655
#4       Bi 0.00775332

情节

您可以创建单独的图。 (在这里,我正在使用一个函数来生成我的图)

plot_food <- function(dd, title=""){
  dd %>%
    ggplot(aes(x = bacteria, y = abundance)) +
    geom_bar(stat = "identity") +
    ggtitle(title)
}
plt_fiber <- plot_food(dd_fiber, "fiber")
plt_fruit <- plot_food(dd_fruit, "fruit")
plt_veg <- plot_food(dd_veg, "veg")
plt_grain <- plot_food(dd_grain, "grain")

然后使用cowplot组合它们

cowplot::plot_grid(plt_fiber, plt_fruit, plt_veg, plt_grain)

【讨论】:

    【解决方案2】:

    TL;DRfacets绘制

    你是如何提出这个问题的,非常不清楚。所以我从

    解释了你的问题

    所以,我想要一个条形图,显示每个类别中每种细菌的数量。因此,每个细菌应该是 4 个图,y 轴为值,x 轴为食物类别。

    作为:

    • 你想要一个条形图
    • 您需要 4 个图,每种细菌类型一个:R、B、P、Bi
    • x 轴 = 食品类别
    • y 轴 = 细菌丰度

    输入

    关于输入数据,数据不清楚,例如您没有描述“样本”、“级别”或“类别”是什么。理想情况下,您会将所有食品类别保存在一个数据框中。例如

    library(tidyr)
    library(dplyr)
    library(ggplot2)
    
    ## The categories you have defined
    bacteria <- c("R", "B", "P", "Bi")
    food <- c("FiberCategory", "FruitCategory", "VegetablesCategory", "WholegrainCategory")
    
    ## Create dummy data for plotting
    set.seed(1)
    num_rows <- length(bacteria)
    num_cols <- length(food)
    dummydata <- 
      matrix(data = abs(rnorm(num_rows*num_cols, mean=0.01, sd=0.05)),
             nrow=num_rows, ncol=num_cols)
    rownames(dummydata) <- bacteria
    colnames(dummydata) <- food
    dummydata <-
      dummydata %>%
      as.data.frame() %>%
      tibble::rownames_to_column("bacteria") %>% 
      gather(food, abundance, -bacteria)
    

    其中的输出如下:

    #> dummydata
    #   bacteria               food   abundance
    #1         R      FiberCategory 0.021322691
    #2         B      FiberCategory 0.019182166
    #3         P      FiberCategory 0.031781431
    #4        Bi      FiberCategory 0.089764040
    #5         R      FruitCategory 0.026475389
    #6         B      FruitCategory 0.031023419
    #7         P      FruitCategory 0.034371453
    #8        Bi      FruitCategory 0.046916235
    #9         R VegetablesCategory 0.038789068
    #10        B VegetablesCategory 0.005269419
    #11        P VegetablesCategory 0.085589058
    #12       Bi VegetablesCategory 0.029492162
    #13        R WholegrainCategory 0.021062029
    #14        B WholegrainCategory 0.100734994
    #15        P WholegrainCategory 0.066246546
    #16       Bi WholegrainCategory 0.007753320
    

    情节

    将数据格式化为上述格式后,您可以简单地执行以下操作:

    dummydata %>%
      ggplot(aes(x = food,
                 y = abundance,
                 group = bacteria)) +
      geom_bar(stat="identity") +
    
      ## Split into 4 plots 
      ## Note: can also use 'facet_grid' to do this
      facet_wrap(~bacteria) +
      theme(
        ## rotate the x-axis label
        axis.text.x = element_text(angle=90, hjust=1, vjust=.5)
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-21
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多