rdataframeggplot2

Sort the y axis on each facet grid in ggplot 2 in R


i have a data frame in R called X .

X
# A tibble: 27 × 6
    Year   delta count color     labels categ
   <int>   <dbl> <int> <chr>     <chr>  <chr>
 1  2024 -0.246     26 red       a/2024 A    
 2  2023 -0.243     37 red       b/2023 A    
 3  2022  0.0490    51 red       c/2022 A    
 4  2020  0.0603   125 red       d/2020 A    
 5  2023 -0.219     24 darkgreen a/2023 B    
 6  2022 -0.185     36 darkgreen b/2022 B    
 7  2024 -0.118     19 darkgreen c/2024 B    
 8  2020 -0.0550    89 darkgreen d/2020 B    
 9  2024 -0.592      9 blue      a/2024 C    
10  2022  0.0336    14 blue      b/2022 C    
# ℹ 17 more rows


i want to plot them using facet grid in ggplot2 but to sort the years in a descending order in the y axis for each category.Staritng from 2024 to 2020.

How can i achieve this sorting in R ?

YEARS <- c(2020, 2022, 2023, 2024)
X%>%
  mutate(Year = factor(Year, levels = YEARS))%>%
  group_by(Year)%>%
  arrange(Year)%>%
  ggplot(aes(x = delta, y = labels#, color = Year
  )) +
  geom_point(size = 3) +
  facet_grid(categ ~., scales="free_y") +
  scale_y_discrete(labels = \(x) str_extract(x, "(?<=/).*")) +
  labs(y="") +
  geom_vline(xintercept=0) +
  theme_bw() +
  theme(legend.position = "none",                           # Remove the legend
        axis.text.x = element_text(angle = 0, hjust = 1),   # Rotate x-axis labels
        strip.text.y = element_text(size = 8, angle = 0, vjust = 0.5),
        axis.text.y  = element_text(size  = 7),
        strip.text = element_text(size = 14),               # Increase facet label size
        axis.title = element_text(size = 14),               # Increase axis title size
        axis.text = element_text(size = 10))+               # Increase axis text size
  theme(strip.background = element_rect(color="black", fill=FACETBACKGROUND, size=1.5, linetype="solid"))+
  labs(title = "",x = "")




resulting to :enter image description here

Data

structure(list(Year = c(2024L, 2023L, 2022L, 2020L, 2023L, 2022L, 
2024L, 2020L, 2024L, 2022L, 2023L, 2020L, 2024L, 2020L, 2022L, 
2020L, 2022L, 2024L, 2023L, 2020L, 2022L, 2023L, 2024L, 2023L, 
2024L, 2020L, 2022L), delta = c(-0.245846153846154, -0.242934362934363, 
0.0490196078431371, 0.0603110504774897, -0.219285714285714, -0.184640522875817, 
-0.118315789473684, -0.0550147922191395, -0.592, 0.0336134453781511, 
0.139047619047619, 0.181280747447187, -0.0920000000000001, 0.561234127400567, 
1.07647058823529, -0.159860728663615, -0.0308464849354378, 0.0329999999999999, 
0.364047619047619, -0.112974017395813, -0.0897631779984723, 0.114805194805195, 
0.237268292682927, -0.38, 0.208, 0.393401959568399, 0.46218487394958
), count = c(26L, 37L, 51L, 125L, 24L, 36L, 19L, 89L, 9L, 14L, 
15L, 33L, 6L, 13L, 5L, 163L, 41L, 16L, 24L, 221L, 77L, 55L, 41L, 
14L, 5L, 88L, 14L), color = c("red", "red", "red", "red", "darkgreen", 
"darkgreen", "darkgreen", "darkgreen", "blue", "blue", "blue", 
"blue", "black", "black", "black", "orange", "orange", "orange", 
"orange", "purple", "purple", "purple", "purple", "#4778BB", 
"#4778BB", "#4778BB", "#4778BB"), labels = c("a/2024", "b/2023", 
"c/2022", "d/2020", "a/2023", "b/2022", "c/2024", "d/2020", "a/2024", 
"b/2022", "c/2023", "d/2020", "a/2024", "b/2020", "c/2022", "a/2020", 
"b/2022", "c/2024", "d/2023", "a/2020", "b/2022", "c/2023", "d/2024", 
"a/2023", "b/2024", "c/2020", "d/2022"), categ = c("A", "A", 
"A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "D", 
"E", "E", "E", "E", "F", "F", "F", "F", "G", "G", "G", "G")), row.names = c(NA, 
-27L), class = c("tbl_df", "tbl", "data.frame"))
> 


Solution

  • You could use your factor column and remove the scale_x_discrete because you already decided the order by factoring the column like this:

    library(tidyverse)
    YEARS <- c(2020, 2022, 2023, 2024)
    X%>%
      mutate(Year = factor(Year, levels = rev(YEARS))) %>%
      group_by(Year)%>%
      arrange(Year)%>%
      ggplot(aes(x = delta, y = Year#, color = Year
      )) +
      geom_point(size = 3) +
      facet_grid(categ ~., scales="free_y") +
      labs(y="") +
      geom_vline(xintercept=0) +
      theme_bw() 
    

    Created on 2024-12-24 with reprex v2.1.0