Placing individual scale limits (on the y-axis) on a chart with facet_wrap

16 Views Asked by At

I am trying to limit the axes of the boxplot graph. I have used the facet_wrap function to group three types of graphs, with different scales, to better visualize the boxplots I need to limit the y axes individually for each graph, however I have not been successful, the way I have tried is the following:

# Establecer la zona horaria (reemplaza "America/La_Paz" con la zona horaria correcta)
Sys.setenv(TZ = "America/La_Paz")

# Data trasnformation
Datos_AOD_CO_NO2_trj_2020_2023_long <- Datos_AOD_CO_NO2_trj_2020_2023 %>%
    gather(key = "Concentration", value = "Value", AOD, CO, NO2) %>%
    mutate(YearMonth = format(as.Date(date, "%m/%d/%Y"), "%Y-%m"),
           Year = format(as.Date(date, "%m/%d/%Y"), "%Y"), Month = fct_inorder(format(as.Date(date, "%m/%d/%Y"), "%B")))


# Custom y axis scale
scales_y <- list(
  `AOD` = scale_y_continuous(limits = c(0.0, 1.25)),
  `CO` = scale_y_continuous(limits = c(11, 57)),
  `NO2` = scale_y_continuous(limits = c(0.0, 0.15))
)


# Boxplot ploting
ggplot(Datos_AOD_CO_NO2_trj_2020_2023_long) +
    geom_boxplot(aes(x = Month, y = Value, fill = Year)) +
    facet_wrap(~ Concentration, nrow = 3, ncol = 1, scales = "free_y")+ stat_summary(aes(x = Month, y = Value, fill = Year), fun = "mean", geom = "point", shape = 8, size = 3, color = "white", position = position_dodge(width = 0.8))+
    labs(x = "Mes", y = "Concentración", title = "Concentraciones de NO2, CO y AOD año 2020-2023") +
    theme(plot.title = element_text(family = 'SansSerif', size = 20, hjust = 0.5),
          axis.title.x = element_text(family = 'SansSerif', face = 'bold', hjust = 0.5),
          axis.title.y = element_text(family = 'SansSerif', face = 'bold', hjust = 0.5)) +
    theme_bw() +
    scale_y_continuous(scales_y)

0

There are 0 best solutions below