Search code examples
rggplot2ggh4x

How to reorder geom_col in ggh4x::facet_wrap2() based off only one of the graphs


I have created a facet_wrap graph and I am trying to reorder the bars from low to high. Obviously that order will be different for the 3 grids, but I want to make them go from low to high BASED off my bottom graph titled "Salt water". Here is my dataset

df = structure(list(Method_Used = c("AnimalBorne_Acoustic", "AnimalBorne_Acoustic_Archival", 
"AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archical", 
"AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archical", 
"AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archical", 
"AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archival_PIT", 
"AnimalBorne_Archival_Satellite", "AnimalBorne_Satellite", "AnimalBorne_Satellite_Archival", 
"AnimalBorne_Satellite_Archival", "BRUV_Acoustic", "BRUV_Acoustic", 
"BRUV_Acoustic", "BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite", 
"Controlled_Acoustic", "Controlled_Acoustic", "Controlled_Acoustic", 
"Controlled_Acoustic", "Controlled_Acoustic", "Controlled_Acoustic", 
"Controlled_Archical", "Controlled_Satellite", "Stationary_Acousitc_PIT", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival", 
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival", 
"Stationary_Acoustic_PIT", "Stationary_Acoustic_Radio", "Stationary_Acoustic_Radio_PIT", 
"Stationary_Archical", "Stationary_Archical", "Stationary_Archical", 
"Stationary_PIT", "Stationary_Radio", "Stationary_Radio", "Stationary_Radio_PIT", 
"Stationary_Satellite", "Stationary_Satellite"), Water_Type = c("Saltwater", 
"Saltwater", "Freshwater", "Saltwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Freshwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater", 
"Freshwater", "Brackish", "Brackish", "Freshwater", "Freshwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Freshwater", 
"Freshwater", "Freshwater", "Saltwater", "Saltwater", "Saltwater", 
"Freshwater", "Freshwater", "Freshwater", "Freshwater", "Saltwater", 
"Saltwater"), Taxa = c("Reptilia", "Chondrichthyes", "Phocidae", 
"Cetacea", "Cetacea", "Chondrichthyes", "Chondrichthyes", "Mollusca", 
"Phocidae", "Reptilia", "Reptilia", "Reptilia", "Teleostei", 
"Reptilia", "Reptilia", "Chondrichthyes", "Chondrichthyes", "Phocidae", 
"Chondrichthyes", "Teleostei", "Teleostei", "Chondrichthyes", 
"Chondrichthyes", "Chondrostei", "Chondrichthyes", "Teleostei", 
"Teleostei", "Teleostei", "Teleostei", "Cetacea", "Phocidae", 
"Teleostei", "Teleostei", "Teleostei", "Teleostei", "Teleostei", 
"Chondrichthyes", "Decapoda", "Teleostei", "Teleostei", "Teleostei", 
"Chondrichthyes", "Decapoda", "Gastropoda", "Teleostei", "Teleostei", 
"Teleostei", "Teleostei", "Gastropoda", "Reptilia", "Teleostei", 
"Teleostei", "Teleostei", "Teleostei", "Teleostei", "Cetacea", 
"Phocidae"), Min_group = c("Medium", "Large", "Not_Reported", 
"Large", "Not_Reported", "Large", "Not_Reported", "Medium", "Not_Reported", 
"Medium", "Not_Reported", "Small", "Large", "Not_Reported", "Large", 
"Large", "Large", "Not_Reported", "Medium", "Large", "Medium", 
"Large", "Medium", "Not_Reported", "Large", "Large", "Medium", 
"Not_Reported", "Small", "Large", "Not_Reported", "Not_Reported", 
"Not_Reported", "Small", "Medium", "Not_Reported", "Not_Reported", 
"Small", "Medium", "Not_Reported", "Small", "Large", "Small", 
"Not_Reported", "Medium", "Small", "Small", "Not_Reported", "Medium", 
"Not_Reported", "Medium", "Not_Reported", "Medium", "Not_Reported", 
"Medium", "Large", "Not_Reported"), total_count = c(1L, 1L, 1L, 
2L, 5L, 5L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 
1L, 1L, 1L, 5L, 4L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -57L))

I tried using the function ~fct_reorder(Method_Used, -total_count, .desc = TRUE) within facet_wrapbut I get this error Error in nrow * ncol : non-numeric argument to binary operator

Here is the code I am using to make the graph

library(dplyr)
library(ggplot2)
library(stringr)
library("ggh4x")
library(forcats)

cols = c("#b218d8", "#7AD151FF","#70EBE9", "#FDE725FF")

Taxa_Plot = ggplot(
  data = df,
  aes(x = Method_Used, y = total_count, 
      fill = factor(Min_group, levels=c('Small', 'Medium', 'Large', "Not_Reported" )))) +
  scale_fill_manual(values = cols, breaks = c('Small', 'Medium', 'Large', "Not_Reported")) +
  geom_col(width=.5) +
  scale_y_continuous(expand = c(0,0)) +
  
  theme_classic() +
  #get multiple graphs, ncol puts them in vertical order instead of horizontal
  #and axes, remove labels keeps the x borders for all graphs
  facet_wrap2(~Water_Type, ncol=1, axes = "all", remove_labels = "all",
              ~fct_reorder(Method_Used, -total_count, .desc = TRUE)) +
  # increase the text size on x and y axis
 theme( axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
        axis.text.y = element_text(size = 10),
        #change size of axis titles
        axis.title.x = element_text(
          size = 20, 
          margin = margin(30, 0, 0, 0)
        ),
        axis.title.y = element_text(
          size = 20, 
          margin = margin(0, 30, 0, 0)),
        #increase spacing between facet wrap plots
        panel.spacing = unit(1.5, "lines"),
        #get rid of border around panel
        strip.background = element_blank(),
        #increase text size of table
        strip.text.x = element_text(size = 15),
        #margin outside of graph, top, right, bottom, left
        plot.margin = margin(1,1,1.5,2, "cm")) +
  # change axis titles
  xlab("Method Used") +
  ylab("Total Number of publications") +
  labs(fill='Size of Individuals Tagged')
Taxa_Plot

Any ideas of how to get them all to reorder based off JUST the bottom graph?


Solution

  • One option would be to use a helper column in fct_reorder where you set the values for non "Saltwater" to zero and use .fun=sum:

    library(dplyr)
    library(ggplot2)
    library(stringr)
    library("ggh4x")
    library(forcats)
    
    cols <- c("#b218d8", "#7AD151FF", "#70EBE9", "#FDE725FF")
    
    df |>
      mutate(Method_Used = fct_reorder(Method_Used,
        ifelse(!Water_Type %in% "Saltwater", 0, total_count),
        .fun = sum
      )) |>
      ggplot(
        aes(
          x = Method_Used, y = total_count,
          fill = factor(Min_group, levels = c("Small", "Medium", "Large", "Not_Reported"))
        )
      ) +
      scale_fill_manual(values = cols, breaks = c("Small", "Medium", "Large", "Not_Reported")) +
      geom_col(width = .5) +
      scale_y_continuous(expand = c(0, 0)) +
      theme_classic() +
      # get multiple graphs, ncol puts them in vertical order instead of horizontal
      # and axes, remove labels keeps the x borders for all graphs
      facet_wrap2(~Water_Type, ncol = 1, axes = "all", remove_labels = "all") +
      # increase the text size on x and y axis
      theme(
        axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
        axis.text.y = element_text(size = 10),
        # change size of axis titles
        axis.title.x = element_text(
          size = 20,
          margin = margin(30, 0, 0, 0)
        ),
        axis.title.y = element_text(
          size = 20,
          margin = margin(0, 30, 0, 0)
        ),
        # increase spacing between facet wrap plots
        panel.spacing = unit(1.5, "lines"),
        # get rid of border around panel
        strip.background = element_blank(),
        # increase text size of table
        strip.text.x = element_text(size = 15),
        # margin outside of graph, top, right, bottom, left
        plot.margin = margin(1, 1, 1.5, 2, "cm")
      ) +
      # change axis titles
      xlab("Method Used") +
      ylab("Total Number of publications") +
      labs(fill = "Size of Individuals Tagged")
    

    enter image description here