ggplot更改某些图例项的大小

5

我有一个带有两个图例的ggplot,一个是点颜色图例,另一个是线型图例。

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

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

示例

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个回答

4

您可以通过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')))

太好了,正是我想要的。非常感谢! - EcologyTom
对我来说,它只在 theme(legend.key.width = unit(5,"cm")) 下工作。 - understorey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接