r - ggplot 更改某些图例项的大小

标签 r ggplot2

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

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

我可以设置整体legend.key.widththeme(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))

最佳答案

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

关于r - ggplot 更改某些图例项的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493293/

相关文章:

r - 比较回归模型与 R

oop - is.object 和 S3 类系统

r - 如何使用 "not equal to"创建函数?

r - 如何在facet_wrap中包含所有轴

r - 使用 Lattice (panel.smoother) 或 ggplot 提取用于获得最佳拟合的方程

r - Shiny R - ggplotly - 当数据集不返回任何信息时显示自定义消息而不是空图

r - Shiny 的 renderUI selectInput 返回 NULL

r - S4 类 : arguments passed to new() don't go into their slots

R 语言感知代码重新格式化/重构工具?

r - Scales = "free"适用于 facet_wrap 但不适用于 facet_grid