1
votes

I'm trying to create a stacked bar chart that will have mean abundance on the y-axis, main trophic group on the x-axis and each bar will be filled by the specific trophic group (the main trophic groups are subdivided further)

I've created an example of my data that you should be able to just put straight into 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"))

I know how to plot this in a bar chart using ggplots if I work out the mean abundances (i.e. the mean number of each species/trophic group across the 3 transects) manually in Excel (but then I don't know how to get my error bars).

How can I summarise this raw data in R so that I can get the mean abundance for each specific trophic group using transects 1-3 as my repeats, which then I can plot in a bar chart as described above?

1

1 Answers

1
votes

I'm not 100% confident this is what you are looking for, but thought I would take a shot.

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