How to stack each element of a stacked barplot in a way that the biggest contributor is on the bottom, and the next biggest on top, etc
08:28 17 Aug 2026

Stacked barplot I get with the code

I have been trying to arrange my stacked barplots in a way that the biggest contributor is on the bottom, and the next biggest in top, etc so that it is successive. Otherwise it looks like a mess.

This is the code I have been using in Rstudio

relabu_summary <- mero %>%
  group_by(Date, Order) %>%
  summarise(Abundance_m2_sum = sum(Abundance_m2, na.rm = TRUE)) %>%
  ungroup() %>%
  group_by(Date) %>%
  mutate(Total_Abundance_m2_rel = sum(Abundance_m2_sum),
         Relative_Abundance = Abundance_m2_sum / Total_Abundance_m2_rel) %>%
  ungroup()


relabu_colors<- c(
  "Bivalvia" = "red", "Bryozoa" = "orange", "Cirripedia" = "darkgreen",
  "Decapoda" = "#7ABF13", "Echinodermata" = "blue", "Gastropoda" = "magenta",
  "Nemertea" = "violet", "Polychaeta" = "lightblue", "Trochophora" = "gray"
) #Colorblind friendly

#Plot
ggplot(relabu_summary, aes(x = Date, y = Relative_Abundance, fill = Order)) +
  geom_bar(stat = "identity", position = "stack", width = 15)+
  labs(title = "Relative Abundance of Orders by Date",
       x = "Date",
       y = "Relative Abundance") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_fill_manual(values = relabu_colors)
ggplot2 bar-chart rstudio stacked