You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot中为多组geom_point与geom_smooth设置不同颜色/色调

调整geom_smooth颜色为比geom_point更深的色调

原始代码与图表

示例代码

data1 <- data.frame(x = 1:15,    # 创建示例数据框
                   y = c(runif(15, 0, 2),
                         runif(15, 2, 6),
                         runif(15, 3, 4),
                         runif(15, 1, 3)),
                   group = rep(LETTERS[1:4], each = 15))

ggplot(data1, aes(x,y, color=group))+
  geom_point() +
geom_smooth(se=FALSE)

生成的图表

原始图表

问题需求

绘制上述图表后,希望将geom_smooth拟合线的颜色设置为比geom_point散点稍深的色调。

解决方法

方法1:手动调整颜色明度

先提取ggplot默认的分组配色,通过降低明度生成深色版本,再分别为散点和拟合线指定颜色:

library(ggplot2)

data1 <- data.frame(x = 1:15,    
                   y = c(runif(15, 0, 2),
                         runif(15, 2, 6),
                         runif(15, 3, 4),
                         runif(15, 1, 3)),
                   group = rep(LETTERS[1:4], each = 15))

# 获取默认的4组颜色
default_cols <- scales::hue_pal()(4)
# 降低明度生成深色(lightness值越小颜色越深,范围0-1)
dark_cols <- scales::adjust_color(default_cols, lightness = 0.7)

ggplot(data1, aes(x, y)) +
  geom_point(aes(color = group)) +
  geom_smooth(aes(color = group), se = FALSE) +
  # 给散点设置默认颜色
  scale_color_manual(values = default_cols, guide = "none") +
  # 给拟合线设置深色,覆盖颜色映射
  scale_color_manual(values = dark_cols, aesthetics = "color")

方法2:用colorspace包快速生成深色

借助colorspace包的darken()函数可以更便捷地调整颜色深浅:

library(ggplot2)
library(colorspace)

data1 <- data.frame(x = 1:15,    
                   y = c(runif(15, 0, 2),
                         runif(15, 2, 6),
                         runif(15, 3, 4),
                         runif(15, 1, 3)),
                   group = rep(LETTERS[1:4], each = 15))

default_cols <- scales::hue_pal()(4)
# amount参数控制加深程度,取值0-1,值越大颜色越深
dark_cols <- darken(default_cols, amount = 0.3)

ggplot(data1, aes(x, y)) +
  geom_point(aes(color = group)) +
  geom_smooth(aes(color = group), se = FALSE) +
  scale_color_manual(values = default_cols) +
  scale_color_manual(values = dark_cols, aesthetics = "color", guide = "none")

方法3:统一图例显示(可选)

如果需要在图例中同时展示散点和拟合线的颜色对应关系,可以通过自定义图例实现:

library(ggplot2)

data1 <- data.frame(x = 1:15,    
                   y = c(runif(15, 0, 2),
                         runif(15, 2, 6),
                         runif(15, 3, 4),
                         runif(15, 1, 3)),
                   group = rep(LETTERS[1:4], each = 15))

default_cols <- scales::hue_pal()(4)
dark_cols <- scales::adjust_color(default_cols, lightness = 0.7)

ggplot(data1, aes(x, y)) +
  geom_point(aes(color = group)) +
  geom_smooth(aes(color = paste0(group, "_dark")), se = FALSE) +
  scale_color_manual(
    values = c(setNames(default_cols, LETTERS[1:4]), setNames(dark_cols, paste0(LETTERS[1:4], "_dark"))),
    guide = guide_legend(
      override.aes = list(
        shape = c(16,16,16,16, NA,NA,NA,NA),
        linetype = c(0,0,0,0, 1,1,1,1)
      ),
      labels = rep(LETTERS[1:4], 2),
      title = "分组"
    )
  ) +
  theme(legend.position = "bottom")

内容的提问来源于stack exchange,提问作者yuliaUU

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 05:35:03