rggplot2facet-wrap

Calculate % contribution and sort ggplot barplot by decreasing % contribution within 2x2 facet windows


I am quite new to this and this is my first time posting on here so apologies if I am missing anything that would be helpful for you to help me.

I am trying to plot the % contribution for species across two time periods in two villages. I am calculating the % contribution for each species by adding the total abundance for a species Village and time_period, and then plotting the % contributions from species across a 2 x 2. I am trying to show only the top 15 species for each Village~time_period as there are many many species for each facet. However, I also want to sort the species by Percent in decreasing order.example of what I am getting 1 another example What I want to see in each plot

I have tried reordering in a number of different ways fct_reorder and reorder_within but none seem to working to order the species correctly, and it is also messing with the X axis alignment.

The code I have tried:

SpeciesI%>% group_by(time_period,Village) %>%
    mutate(Percent=100*TL_abundance/sum(TL_abundance))%>%
    ggplot(aes(x=FinalSpecies, y=Percent))+
    geom_bar( stat="identity")+
    facet_wrap(time_period~Village, scales="free_x")+
    coord_cartesian(xlim =c(1, 15))+
    labs(x="Species", y="Percentage")+
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) 

SpeciesI%>% group_by(time_period,Village) %>%
  mutate(Percent=100*TL_abundance/sum(TL_abundance))%>%
  ggplot(aes(x=reorder_within(FinalSpecies,-Percent,time_period), y=Percent))+
  geom_bar( stat="identity")+
  facet_wrap(time_period~Village, scales="free_x")+
  coord_cartesian(xlim =c(1, 15))+
  scale_x_reordered() +
  labs(x="Species", y="Percentage")+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) 


Solution

  • In stead of trying to sort by facet, you can make 4 individually sorted plots and assemble them with patchwork:

    df <- SpeciesI %>% 
      summarise(.by = c(time_period, Village, FinalSpecies), TL_abundance = sum(TL_abundance)) %>%
      mutate(
        Percent=100*TL_abundance/sum(TL_abundance)
      )
    df
    
    library(patchwork)
    
    p1 <- ggplot(df %>%  
                   filter(Village == "A" & time_period == "2011/12") %>% 
                   mutate(FinalSpecies = fct_reorder(FinalSpecies, Percent, .desc = T)), 
                 aes( x = FinalSpecies, y = Percent)) + 
      geom_col() + 
      labs (title = "A 2011/12", x = "Species", y = "% of Tital Number of Individuals")
    
    p2 <- ggplot(df %>%  
                   filter(Village == "A" & time_period == "2021/22") %>% 
                   mutate(FinalSpecies = fct_reorder(FinalSpecies, Percent, .desc = T)), 
                 aes( x = FinalSpecies, y = Percent)) + 
      geom_col() + 
      labs (title = "A 2021/22", x = "Species", y = "% of Tital Number of Individuals")
    
    p3 <- ggplot(df %>%  
                   filter(Village == "B" & time_period == "2011/12") %>% 
                   mutate(FinalSpecies = fct_reorder(FinalSpecies, Percent, .desc = T)), 
                 aes( x = FinalSpecies, y = Percent)) + 
      geom_col() + 
      labs (title = "B 2011/12", x = "Species", y = "% of Tital Number of Individuals")
    
    p4 <- ggplot(df %>%  
                   filter(Village == "B" & time_period == "2021/22") %>% 
                   mutate(FinalSpecies = fct_reorder(FinalSpecies, Percent, .desc = T)), 
                 aes( x = FinalSpecies, y = Percent)) + 
      geom_col() + 
      labs (title = "B 2021/22",  x = "Species", y = "% of Tital Number of Individuals")
    
    (p1 + p2 + p3 + p4 ) & theme(axis.text.x = element_text(angle = 90, hjust = 1))