【问题标题】:Changing the color of the legend linetype in ggplot在ggplot中更改图例线型的颜色
【发布时间】:2021-08-10 08:16:53
【问题描述】:

我有一个按性别(颜色)和年龄(线型)划分的图表线。生成的图表显示了一个图例,其中两种颜色的实线表示性别,两种 黑色 线类型表示年龄。我想控制线型的颜色(比如绿色而不是黑色)。

有什么想法吗?

df = expand_grid(
  year = c(2010, 2020),
  gender = c('male', 'female'),
  age = c('young', 'old')) %>% 
  mutate(y = 1:8)

ggplot(df, aes(x=year, y = y, color = gender, linetype = age)) + 
  geom_line()

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    一种选择是利用guide_legendoverride.aes 参数:

    library(ggplot2)
    
    ggplot(df, aes(x=year, y = y, color = gender, linetype = age)) + 
      geom_line() +
      guides(linetype = guide_legend(override.aes = list(color = "green")))
    

    【讨论】:

      猜你喜欢
      • 2019-11-12
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 2019-04-17
      • 2021-07-16
      • 2018-01-06
      相关资源
      最近更新 更多