1

I created a line chart using ggplot. I want to increase the vertical spacing between my legend items (text and symbols)

I found this about this problem. However, isn't there a more simple (i.e. without writing a function) solution to this problem?

I tried the command legend.spacing.y but that only moves the entire legend up and down and not the spacing between the items.

Also, I tried: theme(legend.text = element_text(margin = margin(t = 1, unit = "cm")))) However, this only moves the text and not the items.

Could anybody please help?

Thanks!

Dave2e
  • 22,192
  • 18
  • 42
  • 50
nohomejerome
  • 141
  • 1
  • 5

2 Answers2

1

We can also do this in a pipe with tidyverse

library(ggplot2)
library(dplyr)
mtcars %>%
    mutate(cyl = factor(cyl)) %>%
    ggplot(aes(mpg, wt, colour = cyl)) +
   geom_point() +
  theme(
        legend.key.size = unit(1.5, 'lines'),
        legend.key = element_rect(size = 5, color = 'white'))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

This can be useful:

library(ggplot2)
#Code
ggplot(data = mtcars, aes(mpg, wt, colour = factor(cyl))) +
  geom_point() +
  theme(
        legend.key.size = unit(1.5, 'lines'),
        legend.key = element_rect(size = 5, color = 'white'))

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84