Mostrando entradas con la etiqueta margin. Mostrar todas las entradas
Mostrando entradas con la etiqueta margin. Mostrar todas las entradas

2019-06-06

Spacing between legend keys in ggplot2

Problem

We want to add some spacing between the elements of the legend to the following plot using ggplot2.

library(tidyverse)
mtcars %>%
  mutate(transmission = ifelse(am, "manual", "automatic")) %>%
  ggplot() +
  aes(x = transmission, fill = transmission) +
  geom_bar() +
  labs(fill = NULL) +
  theme(
    #legend.spacing.x = unit(.5, "char"), # adds spacing to the left too
    legend.position = "top",
    legend.justification = c(0, 0),
    legend.title = element_blank(),
    legend.margin = margin(c(5, 5, 5, 0))
  )

Solution

  • First option: spacing between the 4 elements
  • We use legend.spacing.x to add the spacing between the 4 elements the legend's key and text.

    mtcars %>%
      mutate(transmission = ifelse(am, "manual", "automatic")) %>%
      ggplot() +
      aes(x = transmission, fill = transmission) +
      geom_bar() +
      labs(fill = NULL) +
      theme(
        legend.spacing.x = unit(.5, "char"),
        legend.position = "top",
        legend.justification = c(0, 0),
        legend.title = element_blank(),
        legend.margin = margin(c(5, 5, 5, 0)))
    
  • Second option: spacing between the 2 groups automatic and manual
  • We pass the argument margin to he element_text function to add spacing only between the 2 groups, the two types of transmissions automatic and manual, between the text automatic and the key (blue square) of manual

    mtcars %>%
      mutate(transmission = ifelse(am, "manual", "automatic")) %>%
      ggplot() +
      aes(x = transmission, fill = transmission) +
      geom_bar() +
      labs(fill = NULL) +
      theme(legend.position = "top",
        legend.justification = c(0, 0),
        legend.title = element_blank(),
        legend.margin = margin(c(5, 5, 5, 0)),
        legend.text = element_text(margin = margin(r = 10, unit = "pt")))
    

    Related posts

    References

    2018-08-12

    Ajustar espacio entre los elementos de una leyenda en ggplot2

    Problema

    Tenemos el siguiente gráfico en el que deseamos separar los dos elementos de la leyenda: automatic y manual.

    library(tidyverse)
    mtcars %>%
      mutate(transmission = ifelse(am, "manual", "automatic")) %>%
      ggplot() +
      aes(x = transmission, fill = transmission) +
      geom_bar() +
      labs(fill = NULL) +
      theme(
        #legend.spacing.x = unit(.5, "char"), # adds spacing to the left too
        legend.position = "top",
        legend.justification = c(0, 0),
        legend.title = element_blank(),
        legend.margin = margin(c(5, 5, 5, 0))
      )
    

    Solución

  • Primera opción: espacio entre los 4 elementos
  • Empleamos legend.spacing.x que crea un espacio entre los 4 elementos, las muestras de color y los textos.

    mtcars %>%
      mutate(transmission = ifelse(am, "manual", "automatic")) %>%
      ggplot() +
      aes(x = transmission, fill = transmission) +
      geom_bar() +
      labs(fill = NULL) +
      theme(
        legend.spacing.x = unit(.5, "char"),
        legend.position = "top",
        legend.justification = c(0, 0),
        legend.title = element_blank(),
        legend.margin = margin(c(5, 5, 5, 0)))
    
  • Segunda opción: espacio entre los 2 grupos de elementos
  • Empleamos margin dentro de element_text para crear un espacio solamente entre los grupos de elementos, el correspondiente a la serie automatic y el correspondiente a manual.

    mtcars %>%
      mutate(transmission = ifelse(am, "manual", "automatic")) %>%
      ggplot() +
      aes(x = transmission, fill = transmission) +
      geom_bar() +
      labs(fill = NULL) +
      theme(legend.position = "top",
        legend.justification = c(0, 0),
        legend.title = element_blank(),
        legend.margin = margin(c(5, 5, 5, 0)),
        legend.text = element_text(margin = margin(r = 10, unit = "pt")))
    

    Entradas relacionadas

    Referencias

    Nube de datos