0

I am generating a two-year boxplot with mean values, however when plotting them the mean values are grouped into a unique different range as shown in the image (the means are displayed with white asterisks).I attach the code used. Does anyone know how I could fix it? Thanks in advance

ggplot(Datos_CO_trj_2021_2022) + 
geom_boxplot(aes(x=fct_inorder(reorder(format(Datos_CO_trj_2021_2022$date,'%B'),Datos_CO_trj_2021_2022$date)), y=Datos_CO_trj_2021_2022$mean, fill=format(Datos_CO_trj_2021_2022$date,'%Y'))) + 
stat_summary(aes(x=fct_inorder(reorder(format(Datos_CO_trj_2021_2022$date,'%B'),Datos_CO_trj_2021_2022$date)), y=Datos_CO_trj_2021_2022$mean, fill=format(Datos_CO_trj_2021_2022$date,'%Y')), fun = "mean", geom = "point", shape = 8, size = 3, color = "white")+labs(x="Mes", y= bquote(CO~"["~mol/m^2~"]"), title = "Concentración de CO año 2021-2022", fill="Año")+ 
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(limits = c(0.01, 0.04))

Boxploti 2021 - 2022 with mean

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
Dani Z
  • 1
  • 1
  • 2
    Add `position = position_dodge(width = 0.8)` inside your `stat_summary` call. You could make your code a lot easier by getting rid of the `Datos_CO_trj_2021_2022$` in front of your variable names inside `aes`, and only using `aes` once inside the actual `ggplot` call – Allan Cameron Aug 21 '23 at 16:49
  • Works! Thank you very much for the advice, I am new to programming in R and with the ggplot2 package, so I have problems with sorting and clearing the variables, I was stuck in that problem for a very long time without knowing how to solve it, I thank you for all your help and knowledge God bless you good man. Pdta: Could I ask something else? How could I change the fill color of only part of the graph, for example from September to February (which are the months in which the values increase)? – Dani Z Aug 21 '23 at 18:20

0 Answers0