0

I want to generate several plots for diferent categories, so I have the following code:

for (módulo in pruebas) {
  plot <- ggplot(data = pruebas.oficiales[pruebas.oficiales$prueba==módulo,],
                 aes(y = Promedio.prueba, x = año)) +
    geom_point(shape = 18, size = 4, 
               color = "#A32F4A") +
    geom_errorbar(data = pruebas.oficiales[pruebas.oficiales$prueba==módulo,],
                  aes(ymin = Promedio.prueba - Desviacion,
                      ymax = Promedio.prueba + Desviacion),
                  color = "#A32F4A", width = 0.4, size = 1.3) +
    theme(
      panel.background = element_rect(fill = "white", 
                                      color = rgb(198, 198, 198, 
                                                  maxColorValue = 255),
                                      size = 1, linetype = "solid"),
      panel.grid.minor = element_line(size = 0.1, linetype = "dashed",
                                      color = rgb(198,198,198, maxColorValue = 255)),
      axis.title = element_text(color = "#575756", family = "Montserrat"),
      axis.text = element_text(family = "Montserrat")
    ) + geom_text(aes(label = Promedio.prueba), hjust = "left",
                  nudge_x = 0.145, show.legend = F, color = "#575756",
                  size = 4, family = "Montserrat") +
    geom_text(aes(label = paste("(", Desviacion, ")")),
              show.legend = F, color = "#575756", hjust = "left",
              vjust = 2, nudge_x = 0.1, size = 4, family = "Montserrat") +
    xlab(NULL) + ylab("Promedio de la prueba")

  assign(paste0("plot.", módulo), plot)
}

If the corresponding "prueba" has four categories for the variable "año" I get a plot like

enter image description here

If the corresponding "prueba" has two categories for the variable "año" I get a plot like

enter image description here

For the latter, the upper labels are not align with the lower labels. Does anyone know how I can make all the plots have aligned labels regardless on the number of categories for "año" they have?

Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
  • Welcome to Stack Overflow! Can you please read and incorporate elements from [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Especially the aspects of using `dput()` for the input and then an explicit example of your expected dataset? – wibeasley Mar 19 '20 at 00:01
  • Have you tried `scale_y_continuos(breaks = unique(pruebas.oficiales$ano))` so that each plot will have the full range of ano? – Croote Mar 19 '20 at 01:10

0 Answers0