rsortingggplot2facet

reorder_within not working with specific columns when facet wrapped


I have an object with engagement data of various members of congress, grouped by year and categorized by party and office. I am extracting from it the top number of engagements by year and ordering it accordingly with the code below.

test <- OfficeRaw %>%
  filter(!is.na(id)) %>%
  count(`Year`,`MoC Name`,`Party Label`, `Congressional Entity`) %>%
  filter(`Party Label` != "Indipendent") %>%
  slice_max(order_by = n,by=Year, n=10) %>%
  ungroup() %>%
  mutate(`MoC Name`=reorder_within(`MoC Name`, n, Year))

Which creates the dataset I am providing here.

structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), levels = c("2019", 
"2020", "2021", "2022", "2023"), class = "factor"), `MoC Name` = structure(c(56L, 
52L, 44L, 38L, 47L, 48L, 49L, 18L, 19L, 24L, 25L, 25L, 44L, 29L, 
32L, 35L, 65L, 64L, 61L, 53L, 57L, 37L, 20L, 53L, 26L, 31L, 63L, 
58L, 55L, 50L, 51L, 39L, 42L, 45L, 46L, 30L, 33L, 34L, 36L, 60L, 
62L, 59L, 54L, 40L, 41L, 21L, 22L, 23L, 27L, 28L, 43L, 12L, 13L, 
14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L), levels = c("Debbie Stabenow___2023", "Dick Durbin___2023", 
"Jake LaTurner___2023", "Jim McGovern___2023", "Joni Ernst___2023", 
"Marco Rubio___2023", "Mitch McConnell___2023", "Raphael Warnock___2023", 
"Robert Aderholt___2023", "Sherrod Brown___2023", "Tom Kean___2023", 
"Colin Allred___2023", "Cory Booker___2023", "Earl Blumenauer___2023", 
"John Boozman___2023", "Mike Braun___2023", "Sara Jacobs___2023", 
"Cory Booker___2019", "Dean Phillips___2019", "Dean Phillips___2020", 
"Gregory Meeks___2022", "Hal Rogers___2022", "Jackie Walorski___2022", 
"Jeff Merkley___2019", "Jim Costa___2019", "Jim McGovern___2020", 
"Jim Risch___2022", "Joaquin Castro___2022", "K. Michael Conaway___2019", 
"Karen Bass___2021", "Lois Frankel___2020", "Marco Rubio___2019", 
"Marco Rubio___2021", "Michael McCaul___2021", "Roger Marshall___2019", 
"Sara Jacobs___2021", "Betty McCollum___2020", "Chrissy Houlahan___2019", 
"Darin LaHood___2021", "Darin LaHood___2022", "Earl Blumenauer___2022", 
"Gregory Meeks___2021", "Jeff Merkley___2023", "Jim McGovern___2019", 
"Jim McGovern___2021", "Joaquin Castro___2021", "Pat Roberts___2019", 
"Ted Lieu___2019", "Todd Young___2019", "Betty McCollum___2021", 
"Cory Booker___2021", "Ilhan Omar___2019", "Jackie Walorski___2020", 
"Bob Casey___2022", "Earl Blumenauer___2021", "John Boozman___2019", 
"Xochitl Torres Small___2020", "Grace Meng___2021", "Sara Jacobs___2022", 
"Betty McCollum___2022", "Darin LaHood___2020", "Grace Meng___2022", 
"Jackie Walorski___2021", "Bob Casey___2020", "Earl Blumenauer___2020"
), class = "factor", scores = structure(c(`Betty McCollum___2020` = 6, 
`Betty McCollum___2021` = 7, `Betty McCollum___2022` = 10, `Bob Casey___2020` = 11, 
`Bob Casey___2022` = 8, `Chrissy Houlahan___2019` = 6, `Colin Allred___2023` = 4, 
`Cory Booker___2019` = 5, `Cory Booker___2021` = 7, `Cory Booker___2023` = 4, 
`Darin LaHood___2020` = 10, `Darin LaHood___2021` = 6, `Darin LaHood___2022` = 6, 
`Dean Phillips___2019` = 5, `Dean Phillips___2020` = 5, `Debbie Stabenow___2023` = 3, 
`Dick Durbin___2023` = 3, `Earl Blumenauer___2020` = 13, `Earl Blumenauer___2021` = 8, 
`Earl Blumenauer___2022` = 6, `Earl Blumenauer___2023` = 4, `Grace Meng___2021` = 9, 
`Grace Meng___2022` = 10, `Gregory Meeks___2021` = 6, `Gregory Meeks___2022` = 5, 
`Hal Rogers___2022` = 5, `Ilhan Omar___2019` = 7, `Jackie Walorski___2020` = 7.5, 
`Jackie Walorski___2021` = 10, `Jackie Walorski___2022` = 5, 
`Jake LaTurner___2023` = 3, `Jeff Merkley___2019` = 5, `Jeff Merkley___2023` = 6, 
`Jim Costa___2019` = 5, `Jim McGovern___2019` = 6, `Jim McGovern___2020` = 5, 
`Jim McGovern___2021` = 6, `Jim McGovern___2023` = 3, `Jim Risch___2022` = 5, 
`Joaquin Castro___2021` = 6, `Joaquin Castro___2022` = 5, `John Boozman___2019` = 8, 
`John Boozman___2023` = 4, `Joni Ernst___2023` = 3, `K. Michael Conaway___2019` = 5, 
`Karen Bass___2021` = 5, `Lois Frankel___2020` = 5, `Marco Rubio___2019` = 5, 
`Marco Rubio___2021` = 5, `Marco Rubio___2023` = 3, `Michael McCaul___2021` = 5, 
`Mike Braun___2023` = 4, `Mitch McConnell___2023` = 3, `Pat Roberts___2019` = 6, 
`Raphael Warnock___2023` = 3, `Robert Aderholt___2023` = 3, `Roger Marshall___2019` = 5, 
`Sara Jacobs___2021` = 5, `Sara Jacobs___2022` = 9, `Sara Jacobs___2023` = 4, 
`Sherrod Brown___2023` = 3, `Ted Lieu___2019` = 6, `Todd Young___2019` = 6, 
`Tom Kean___2023` = 3, `Xochitl Torres Small___2020` = 8), dim = 65L, dimnames = list(
    c("Betty McCollum___2020", "Betty McCollum___2021", "Betty McCollum___2022", 
    "Bob Casey___2020", "Bob Casey___2022", "Chrissy Houlahan___2019", 
    "Colin Allred___2023", "Cory Booker___2019", "Cory Booker___2021", 
    "Cory Booker___2023", "Darin LaHood___2020", "Darin LaHood___2021", 
    "Darin LaHood___2022", "Dean Phillips___2019", "Dean Phillips___2020", 
    "Debbie Stabenow___2023", "Dick Durbin___2023", "Earl Blumenauer___2020", 
    "Earl Blumenauer___2021", "Earl Blumenauer___2022", "Earl Blumenauer___2023", 
    "Grace Meng___2021", "Grace Meng___2022", "Gregory Meeks___2021", 
    "Gregory Meeks___2022", "Hal Rogers___2022", "Ilhan Omar___2019", 
    "Jackie Walorski___2020", "Jackie Walorski___2021", "Jackie Walorski___2022", 
    "Jake LaTurner___2023", "Jeff Merkley___2019", "Jeff Merkley___2023", 
    "Jim Costa___2019", "Jim McGovern___2019", "Jim McGovern___2020", 
    "Jim McGovern___2021", "Jim McGovern___2023", "Jim Risch___2022", 
    "Joaquin Castro___2021", "Joaquin Castro___2022", "John Boozman___2019", 
    "John Boozman___2023", "Joni Ernst___2023", "K. Michael Conaway___2019", 
    "Karen Bass___2021", "Lois Frankel___2020", "Marco Rubio___2019", 
    "Marco Rubio___2021", "Marco Rubio___2023", "Michael McCaul___2021", 
    "Mike Braun___2023", "Mitch McConnell___2023", "Pat Roberts___2019", 
    "Raphael Warnock___2023", "Robert Aderholt___2023", "Roger Marshall___2019", 
    "Sara Jacobs___2021", "Sara Jacobs___2022", "Sara Jacobs___2023", 
    "Sherrod Brown___2023", "Ted Lieu___2019", "Todd Young___2019", 
    "Tom Kean___2023", "Xochitl Torres Small___2020")))), `Party Label` = c("Republican", 
"Democrat", "Democrat", "Democrat", "Republican", "Democrat", 
"Republican", "Democrat", "Democrat", "Democrat", "Democrat", 
"Democrat", "Democrat", "Republican", "Republican", "Republican", 
"Democrat", "Democrat", "Republican", "Republican", "Democrat", 
"Democrat", "Democrat", "Republican", "Democrat", "Democrat", 
"Republican", "Democrat", "Democrat", "Democrat", "Democrat", 
"Republican", "Democrat", "Democrat", "Democrat", "Democrat", 
"Republican", "Republican", "Democrat", "Democrat", "Democrat", 
"Democrat", "Democrat", "Republican", "Democrat", "Democrat", 
"Republican", "Republican", "Republican", "Democrat", "Democrat", 
"Democrat", "Democrat", "Democrat", "Republican", "Republican", 
"Democrat", "Democrat", "Democrat", "Republican", "Democrat", 
"Republican", "Republican", "Republican", "Democrat", "Republican", 
"Democrat", "Republican"), `Congressional Entity` = c("Senate", 
"House", "House", "House", "Senate", "House", "Senate", "Senate", 
"House", "Senate", "House", "Senate", "Senate", "Senate", "Senate", 
"Senate", "House", "Senate", "House", "House", "House", "Senate", 
"Senate", "Senate", "Senate", "Senate", "House", "House", "House", 
"House", "Senate", "House", "House", "House", "House", "House", 
"Senate", "House", "House", "House", "House", "House", "Senate", 
"House", "House", "House", "House", "House", "Senate", "House", 
"Senate", "House", "Senate", "House", "Senate", "Senate", "House", 
"Senate", "Senate", "House", "House", "Senate", "Senate", "Senate", 
"Senate", "House", "Senate", "House"), n = c(8L, 7L, 7L, 6L, 
6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 13L, 11L, 10L, 
10L, 8L, 6L, 5L, 5L, 5L, 5L, 10L, 9L, 8L, 7L, 7L, 6L, 6L, 6L, 
6L, 5L, 5L, 5L, 5L, 10L, 10L, 9L, 8L, 6L, 6L, 5L, 5L, 5L, 5L, 
5L, 6L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L)), row.names = c(NA, -68L), class = c("tbl_df", "tbl", 
"data.frame"))

I have been trying to create a descending horizontal graph that will order this list of top ten engagements by year, using the code below:

test %>%
ggplot(aes(n,`MoC Name`, fill = `Party Label`), stat = "identity") +
  geom_col() + 
  facet_wrap(~Year, scales = 'free_y') +
  scale_y_reordered()

Which results in this:

ImageOutput

The graph is out of order and I have been trying to reorder those odd columns for a while to no avail, using fct_inorder, changing the position of reorder_within, and even falling back to the older reorder function. I am not sure how I can fix the issue at this point.

Any help or redirection to relevant information is greatly appreciated!


Solution

  • The issue is that you grouped by a fourth variable aka Congressional Entity when computing the counts. As a consequence you have some duplicated pairs of MoC Name and Year for which reorder_within can not account for:

    library(dplyr, warn = FALSE)
    
    # Duplicates
    
    test |> 
      count(Year, `MoC Name`) |> 
      filter(n > 1)
    #> # A tibble: 3 × 3
    #>   Year  `MoC Name`                 n
    #>   <fct> <fct>                  <int>
    #> 1 2019  Jim Costa___2019           2
    #> 2 2019  Jim McGovern___2019        2
    #> 3 2020  Jackie Walorski___2020     2
    

    To fix that drop Congressional Entity when computing the counts, i.e. do

    count(`Year`,`MoC Name`,`Party Label`) 
    

    As I don't have your raw data the reprex below uses the already wrangled data to achieve the same result. That's why I had to add wt = n to compute the counts based on the counts in the wrangled data:

    library(ggplot2)
    library(tidytext)
    
    test1 <- test |>
      mutate(`MoC Name` = gsub("_.*$", "", `MoC Name`)) |>
      count(`Year`, `MoC Name`, `Party Label`, wt = n) |>
      mutate(`MoC Name` = reorder_within(`MoC Name`, n, Year))
    
    test1 |>
      ggplot(aes(n, `MoC Name`, fill = `Party Label`), stat = "identity") +
      geom_col() +
      scale_y_reordered() +
      facet_wrap(~Year, scales = "free_y")