0

I am trying to add a general legend to facets in a plot. But I can´t. I was looking but the nearest question is this: How do you add a general label to facets in ggplot2?. Anyway, this it isn't an answer for my problem because I hace discrete data. The first answer seems to be a good answer but, there is a way to make that a function to avoid write this code everytime that I plot similar data? I will make around 24 plots, so it will helpfull if there is a way.

Data

prueba <- structure(list(
  UnidadD = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 
              5L, 5L, 5L, 5L), 
  Lección.de.dificultad = c(
    "3. Funciones trigonométricas", "4. Práctica en GeoGebra", 
    "4. Práctica en GeoGebra", "4. Práctica en GeoGebra", 
    "1. Función biyectiva e inversa", "3. Funciones trigonométricas", 
    "2. Función logarítmica", "1. Función biyectiva e inversa", 
    "3. Funciones trigonométricas", "3. Funciones trigonométricas", 
    "4. Práctica en GeoGebra", "3. Funciones trigonométricas", 
    "2. Funciones y ecuaciones exponenciales", "3. Funciones trigonométricas", 
    "3. Funciones trigonométricas", "3. Funciones trigonométricas", 
    "3. Funciones trigonométricas", "3. Funciones trigonométricas", 
    "3. Funciones trigonométricas", "3. Funciones trigonométricas"
  )
), class = "data.frame", row.names = c(NA, -20L))

Graph

prueba %>% 
  group_by(UnidadD, Lección.de.dificultad) %>%
  summarise(n = n()) %>%
  arrange(desc(Lección.de.dificultad)) %>%
  ungroup() %>%
  mutate(Lección.de.dificultad=factor(Lección.de.dificultad, levels=Lección.de.dificultad)) %>%
  ggplot(aes(Lección.de.dificultad, n)) +
  geom_col(fill = '#5081ac') +
  facet_grid(UnidadD~., scales = "free", space = 'free') +
  coord_flip() +
  xlab('Lecciones con mayor dificultad') +
  ylab('Cantidad de participantes') +
  theme(axis.line = element_line(colour = 'black'),
        panel.background = element_blank(),
        axis.text = element_text(size = 16),
        axis.title = element_text(size = 18),
        panel.grid.major.y = element_line(colour = 'gray'),
        panel.grid.minor.y = element_line(colour = 'gray'),
        plot.title = element_text(hjust = 0.5,
                                  size = 18),
        text = element_text(family = 'Sans'),
        strip.background = element_rect(fill = "#afc7e3"),
        strip.text = element_text(size = 14))

Update: I resolve my problem using ggh4x package. Here is the code.

prueba[(prueba$Grado == 11) & (prueba$BC == "Funciones"),] %>% 
  group_by(UnidadD, Lección.de.dificultad) %>%
  summarise(n = n()) %>%
  arrange(desc(Lección.de.dificultad)) %>%
  ungroup() %>%
  mutate(Lección.de.dificultad=factor(Lección.de.dificultad, levels=Lección.de.dificultad)) %>%
  ggplot(aes(Lección.de.dificultad, n)) +
  geom_col(fill = '#5081ac') +
  ggh4x::facet_nested("Unidad" + UnidadD~., scales = "free", space = "free") +
  coord_flip() +
  xlab('Lecciones con mayor dificultad') +
  ylab('Cantidad de participantes') +
  theme(axis.line = element_line(colour = 'black'),
        panel.background = element_blank(),
        axis.text = element_text(size = 16),
        axis.title = element_text(size = 18),
        panel.grid.major.y = element_line(colour = 'gray'),
        panel.grid.minor.y = element_line(colour = 'gray'),
        plot.title = element_text(hjust = 0.5,
                                  size = 18),
        text = element_text(family = 'Sans'),
        strip.background = element_rect(fill = "#afc7e3"),
        strip.text = element_text(size = 16)) +
  labs(title = 'Funciones (Segundo año de bachillerato)')

0 Answers0