How do I increase the vertical spacing between legend key elements?

Hi,

How do I increase the vertical spacing between legend key elements?

  1. How would I do these in both legends below?
  2. How might I do this in just 1 legend below?
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.3.2
#> Warning: package 'purrr' was built under R version 4.3.1
#> Warning: package 'dplyr' was built under R version 4.3.1
#> Warning: package 'forcats' was built under R version 4.3.2
#> Warning: package 'lubridate' was built under R version 4.3.1
library(palmerpenguins)

penguins |>
  group_by(sex) |>
  summarise(flipper_length_mm = mean(flipper_length_mm, na.rm = T)) |>
  ggplot(aes(x = sex, y = flipper_length_mm, fill = sex)) +
  geom_col() +
  geom_point(aes(shape = sex)) +
  facet_wrap(~sex) +
  theme(legend.text = element_text(margin = margin())) +
  labs(fill = "Legend 2", shape = "Legend 1")
#> Warning: Removed 1 rows containing missing values (`geom_point()`).

Created on 2023-11-23 with reprex v2.0.2

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
library(palmerpenguins)

object <- penguins |>
  group_by(sex) |>
  summarise(flipper_length_mm = mean(flipper_length_mm, na.rm = T)) |>
  ggplot(aes(x = sex, y = flipper_length_mm, fill = sex)) +
  geom_col() +
  geom_point(aes(shape = sex)) +
  facet_wrap(~sex) +
  theme(legend.text = element_text(margin = margin())) +
  labs(fill = "Legend 2", shape = "Legend 1")

object +
  theme(legend.spacing.y = unit(2.5, 'mm')) + 
  guides(fill = guide_legend(byrow = TRUE))
#> Warning: Removed 1 rows containing missing values (`geom_point()`).

Created on 2023-11-22 with reprex v2.0.2

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.