Ordering for stacked bar chart but with different values in each one.

You need to convert Phylum to a factor, ordered by the mean abundance. I think this will do it.

d3 <- d3 %>% 
  group_by(Phylum) %>% 
  mutate(
    mean = mean(`Relative abundance`)
  ) %>% 
  ungroup() %>% 
  arrange(desc(mean)) %>% 
  mutate(
    Phylum = factor(Phylum, levels = unique(Phylum))
  )
1 Like