4
votes

I have a ggplot with two legends, one for point colour and one for linetype.

I would like to extend the width of only the linetype legend key.

I can set the overall legend.key.width with theme(legend.key.width = unit(5, "cm")), but this increases the width for both legends. Is there a way to set the width for just one of the legends?

Example

iris$Group <- as.factor(rep(1:3, 50))

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point(aes(colour = Species)) +
  geom_line(aes(linetype = Group))
1

1 Answers

3
votes

You can specify options for each legend type independently by guides() function:

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point(aes(colour = Species)) +
  geom_line(aes(linetype = Group)) +
  guides(linetype = guide_legend(keywidth = unit(5, 'cm')))