创建具有平均丰度的堆积条形图

Creating stacked bar-charts with mean abundance

我正在尝试创建一个堆积条形图,y 轴为平均丰度,x 轴为主要营养组,每个条形图将由特定营养组填充(主要营养组进一步细分)

我创建了一个数据示例,您应该可以直接将其放入 R 中:

Example<-structure(list(Species = c("Fish1", "Fish2", "Fish3", "Fish4", 
"Fish5", "Fish6", "Fish7", "Fish1", "Fish2", "Fish3", "Fish4", 
"Fish5", "Fish6", "Fish7", "Fish1", "Fish2", "Fish3", "Fish4", 
"Fish5", "Fish6", "Fish7"), Trophic = c("Herbivore", "Omnivore", 
"Herbivore", "Predator", "Predator", "Omnivore", "Omnivore", 
"Herbivore", "Omnivore", "Herbivore", "Predator", "Predator", 
"Omnivore", "Omnivore", "Herbivore", "Omnivore", "Herbivore", 
"Predator", "Predator", "Omnivore", "Omnivore"), Trophic_Specific = c("Grazer", 
"Generalist_Omnivore", "Browser", "Micro-invertebrate_Predator", 
"Micro-invertebrate_Predator", "Generalist_Omnivore", "Benthic_Omnivore", 
"Grazer", "Generalist_Omnivore", "Browser", "Micro-invertebrate_Predator", 
"Micro-invertebrate_Predator", "Generalist_Omnivore", "Benthic_Omnivore", 
"Grazer", "Generalist_Omnivore", "Browser", "Micro-invertebrate_Predator", 
"Micro-invertebrate_Predator", "Generalist_Omnivore", "Benthic_Omnivore"
), Transect = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 
3, 3, 3, 3, 3, 3), Count = c(1, 2, 34, 0, 4, 2, 1, 0, 2, 25, 
1, 4, 2, 1, 1, 4, 50, 3, 6, 7, 3)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -21L), spec = structure(list(
    cols = list(Species = structure(list(), class = c("collector_character", 
    "collector")), Trophic = structure(list(), class = c("collector_character", 
    "collector")), Trophic_Specific = structure(list(), class = c("collector_character", 
    "collector")), Transect = structure(list(), class = c("collector_double", 
    "collector")), Count = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

如果我在 Excel(但后来我不知道如何得到我的错误栏)。

我如何在 R 中总结这些原始数据,以便我可以使用横断面 1-3 作为我的重复获得每个特定营养组的平均丰度,然后我可以如上所述在条形图中绘制?

我不是 100% 确信这就是您要找的东西,但我想我会试一试。

library(tidyverse)

Example %>%
  group_by(Trophic, Trophic_Specific) %>%
  summarise(Mean = mean(Count),
            SD = sd(Count),
            n = n(),
            SE = SD/n)

# A tibble: 5 x 6
# Groups:   Trophic [3]
  Trophic   Trophic_Specific              Mean     SD     n    SE
  <chr>     <chr>                        <dbl>  <dbl> <int> <dbl>
1 Herbivore Browser                     36.3   12.7       3 4.22 
2 Herbivore Grazer                       0.667  0.577     3 0.192
3 Omnivore  Benthic_Omnivore             1.67   1.15      3 0.385
4 Omnivore  Generalist_Omnivore          3.17   2.04      6 0.340
5 Predator  Micro-invertebrate_Predator  3      2.19      6 0.365