Home > Mobile >  How to reorder categorical variables in x axis in ggplot2?
How to reorder categorical variables in x axis in ggplot2?

Time:03-23

I have a data frame called S1 and plotted it using ggplot2. In the categorical variables in the x-axis plots, I want to reorder them like this series("Mar", "Apr", "May", "Jun", "Jul", "AMJ", "MAMJJ").

Any thoughts or ideas, please?

enter image description here

Here is the code I used:

S1 <- weather_indices %>% filter(season == "season1", variable == "rainfall") %>% 
    filter(str_detect(source, 'Mada|IDW2')) 
> head(S1)
  year aggregation_period   value source statistic variable  season
1 2001                Mar 189.000  Mada4       sum rainfall season1
2 2001                Mar 249.000 Mada22       sum rainfall season1
3 2001                Mar 174.000 Mada38       sum rainfall season1
4 2001                Mar 286.000 Mada53       sum rainfall season1
5 2001                Mar 207.654   IDW2       sum rainfall season1
6 2001                Apr 210.000  Mada4       sum rainfall season1
>  
>  S1 %>%  ggplot(aes(x = aggregation_period  , y = value))  
    geom_boxplot(width = 0.3, col = "black", )  
    facet_grid(cols = vars(source), rows = vars(season) ,scales = "free_y")  
    scale_y_continuous(breaks = seq(0, 1350, 175))  
    xlab("Rainfall Indices")  
    ylab("Rainfall (mm)")  
    theme_bw()  
    theme(axis.title = element_text(size = 12),  # all titles 
          axis.text = element_text(colour = "black"),
          axis.text.x = element_text(angle = 90, hjust = 1,
                                     size = 10, color = "black"),
          # axis.text.y = element_text(size = 10),
          panel.border = element_rect(color = "black",
                                      size = .5))

and here is the dput of my data:

dput(S1_sub)
structure(list(year = c(2001, 2001, 2001, 2001, 2001, 2002, 2002, 
2002, 2002, 2002, 2003, 2003, 2003, 2003, 2003, 2004, 2004, 2004, 
2004, 2004, 2005, 2005, 2005, 2005, 2005, 2001, 2002, 2003, 2004, 
2005, 2001, 2002, 2003, 2004, 2005), aggregation_period = c("Mar", 
"Apr", "May", "Jun", "Jul", "Mar", "Apr", "May", "Jun", "Jul", 
"Mar", "Apr", "May", "Jun", "Jul", "Mar", "Apr", "May", "Jun", 
"Jul", "Mar", "Apr", "May", "Jun", "Jul", "MAMJJ", "MAMJJ", "MAMJJ", 
"MAMJJ", "MAMJJ", "AMJ", "AMJ", "AMJ", "AMJ", "AMJ"), value = c(189, 
210, 141, 175, 100, 61, 317, 93, 297, 213, 185, 78, 102, 249, 
205, 114, 214, 158, 339, 108, 99.545, 115.825, 219.66, 99.622, 
129.832, 865.175, 981, 819, 874, 817, 657.521, 920, 634, 793, 
646), source = c("Mada4", "Mada4", "Mada4", "Mada4", "Mada4", 
"Mada22", "Mada22", "Mada22", "Mada22", "Mada22", "Mada38", "Mada38", 
"Mada38", "Mada38", "Mada38", "Mada53", "Mada53", "Mada53", "Mada53", 
"Mada53", "IDW2", "IDW2", "IDW2", "IDW2", "IDW2", "IDW2", "Mada22", 
"Mada38", "Mada4", "Mada53", "IDW2", "Mada22", "Mada38", "Mada4", 
"Mada53"), statistic = c("sum", "sum", "sum", "sum", "sum", "sum", 
"sum", "sum", "sum", "sum", "sum", "sum", "sum", "sum", "sum", 
"sum", "sum", "sum", "sum", "sum", "sum", "sum", "sum", "sum", 
"sum", "sum", "sum", "sum", "sum", "sum", "sum", "sum", "sum", 
"sum", "sum"), variable = c("rainfall", "rainfall", "rainfall", 
"rainfall", "rainfall", "rainfall", "rainfall", "rainfall", "rainfall", 
"rainfall", "rainfall", "rainfall", "rainfall", "rainfall", "rainfall", 
"rainfall", "rainfall", "rainfall", "rainfall", "rainfall", "rainfall", 
"rainfall", "rainfall", "rainfall", "rainfall", "rainfall", "rainfall", 
"rainfall", "rainfall", "rainfall", "rainfall", "rainfall", "rainfall", 
"rainfall", "rainfall"), season = c("season1", "season1", "season1", 
"season1", "season1", "season1", "season1", "season1", "season1", 
"season1", "season1", "season1", "season1", "season1", "season1", 
"season1", "season1", "season1", "season1", "season1", "season1", 
"season1", "season1", "season1", "season1", "season1", "season1", 
"season1", "season1", "season1", "season1", "season1", "season1", 
"season1", "season1")), row.names = c(NA, -35L), class = "data.frame")

CodePudding user response:

We could use fct_relevel from forcats package, it is in tidyverse:

library(tidyverse)

S1_sub %>% filter(season == "season1", variable == "rainfall") %>% 
     filter(str_detect(source, 'Mada|IDW2')) %>% 
  mutate(aggregation_period = fct_relevel(aggregation_period, c("Mar", "Apr", "May", "Jun", "Jul", "AMJ", "MAMJJ"))) %>% 
  ggplot(aes(x = aggregation_period  , y = value))  
     geom_boxplot(width = 0.3, col = "black", )  
     facet_grid(cols = vars(source), rows = vars(season) ,scales = "free_y")  
     scale_y_continuous(breaks = seq(0, 1350, 175))  
     xlab("Rainfall Indices")  
     ylab("Rainfall (mm)")  
     theme_bw()  
     theme(axis.title = element_text(size = 12),  # all titles 
                     axis.text = element_text(colour = "black"),
                     axis.text.x = element_text(angle = 90, hjust = 1,
                                                size = 10, color = "black"),
                     # axis.text.y = element_text(size = 10),
                       panel.border = element_rect(color = "black",
                                                   size = .5))

enter image description here

CodePudding user response:

You can reorder the factor, the order of factor levels is used in ggplot.

s1$aggregation_period  <- factor(s1$aggregation_periody,levels = c("Mar", "Apr", "May", "Jun", "Jul", "AMJ", "MAMJJ"))
  • Related