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

Posted

技术标签:

【中文标题】创建具有平均丰度的堆叠条形图【英文标题】:Creating stacked bar-charts with mean abundance 【发布时间】:2020-05-05 19:44:12 【问题描述】:

我正在尝试创建一个堆叠条形图,它在 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 中手动计算出平均丰度(即 3 个样带中每个物种/营养组的平均数量),我知道如何使用 ggplots 在条形图中绘制它(但我不知道如何得到我的误差线)。

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

【问题讨论】:

【参考方案1】:

我不是 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

【讨论】:

这正是我想要的!非常感谢!

以上是关于创建具有平均丰度的堆叠条形图的主要内容,如果未能解决你的问题,请参考以下文章

具有重复颜色的 HTML5 图表堆叠条形图

R中具有多列整数的堆积条形图

创建具有多个维度的堆积条形图

ggplot - 如何堆叠和分组条形图?

如何使用 Seaborn 创建 FacetGrid 堆叠条形图?

创建 100% 堆叠条形图