ggplot change size of some legend items

Eco*_*Tom 5 r ggplot2

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

我想扩展线型图例键的宽度。

legend.key.width我可以使用来设置整体theme(legend.key.width = unit(5, "cm")),但这会增加两个图例的宽度。有没有办法只设置其中一个图例的宽度?

例子

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))
Run Code Online (Sandbox Code Playgroud)

Paw*_*ros 5

您可以按功能独立指定每种图例类型的选项guides()

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')))
Run Code Online (Sandbox Code Playgroud)