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

ggplot图表图例不显示问题求助,求添加回归方程与R²

问题解决:ggplot图例显示+添加回归方程与R²

一、修复图例不显示问题

你的代码核心问题是没有在aes()中正确映射图例标签,而是直接传入了颜色/形状/线型的具体值,导致ggplot无法识别这些元素需要生成图例。以下是修正后的代码:

library(ggplot2)

ggplot(clean_1br, aes(x = as.Date(Date), y = LN_Conc)) + 
  # 散点图:aes中传入图例标签,而非直接颜色/形状值
  geom_point(aes(color = 'LN Conc', shape = 'LN Conc')) +
  # 回归线:aes中传入图例标签
  geom_smooth(aes(color = 'Linear (LN Conc)', linetype = 'Linear (LN Conc)'), 
              method = 'lm', se = TRUE, level = 0.90, fill = 'lightgrey') +
  # 水平线:移除aes外的color参数,避免覆盖映射;aes中传入图例标签
  geom_hline(aes(color = 'LN Screening Level', linetype = 'LN Screening Level'), 
             yintercept = 6.5) +
  labs(title = 'Determination of First-Order Rate Constant', 
       x = 'Date', 
       y = expression(paste("LN Conc (",mu,"g/L)", sep="")),
       # 统一设置图例标题(可选,去掉则用默认标签)
       color = 'Legend', shape = 'Legend', linetype = 'Legend') +
  ylim(6.5, 8) +
  # 颜色映射:标签对应你要的颜色值
  scale_color_manual(values = c('LN Conc' = '#2A788E', 
                                'Linear (LN Conc)' = 'darkgrey', 
                                'LN Screening Level' = 'black')) +
  # 形状映射:仅散点需要形状,其他设为NA
  scale_shape_manual(values = c('LN Conc' = 20, 
                                'Linear (LN Conc)' = NA, 
                                'LN Screening Level' = NA)) +
  # 线型映射:回归线和水平线设置对应线型,散点设为NA
  scale_linetype_manual(values = c('LN Conc' = NA, 
                                   'Linear (LN Conc)' = 'solid', 
                                   'LN Screening Level' = 'dashed')) +
  theme_classic() +
  theme(legend.position = "bottom", legend.box = "horizontal")

关键修改点:

  • 所有需要在图例中显示的元素,都在aes()中传入自定义标签文本(如'LN Conc'),而非直接的颜色/形状/线型值
  • 移除geom_hline中aes()外的color = 'black',避免覆盖aes中的映射关系
  • 通过labs()统一设置三个美学属性(color/shape/linetype)的图例标题,让图例合并显示(可选,若不需要统一标题可删除该行)

二、添加回归方程与R²值

可以使用ggpmisc包的stat_poly_eq()函数自动计算并添加回归方程和R²,步骤如下:

  1. 先安装并加载包:
install.packages("ggpmisc")
library(ggpmisc)
  1. 在原有代码基础上添加stat_poly_eq()层:
library(ggplot2)
library(ggpmisc)

ggplot(clean_1br, aes(x = as.Date(Date), y = LN_Conc)) + 
  geom_point(aes(color = 'LN Conc', shape = 'LN Conc')) +
  geom_smooth(aes(color = 'Linear (LN Conc)', linetype = 'Linear (LN Conc)'), 
              method = 'lm', se = TRUE, level = 0.90, fill = 'lightgrey') +
  geom_hline(aes(color = 'LN Screening Level', linetype = 'LN Screening Level'), 
             yintercept = 6.5) +
  # 添加回归方程和R²
  stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
               formula = y ~ x,
               parse = TRUE,
               size = 4,
               color = "darkgrey",
               position = position_nudge(x = 10)) # 调整文本位置,避免遮挡
  labs(title = 'Determination of First-Order Rate Constant', 
       x = 'Date', 
       y = expression(paste("LN Conc (",mu,"g/L)", sep="")),
       color = 'Legend', shape = 'Legend', linetype = 'Legend') +
  ylim(6.5, 8) +
  scale_color_manual(values = c('LN Conc' = '#2A788E', 
                                'Linear (LN Conc)' = 'darkgrey', 
                                'LN Screening Level' = 'black')) +
  scale_shape_manual(values = c('LN Conc' = 20, 
                                'Linear (LN Conc)' = NA, 
                                'LN Screening Level' = NA)) +
  scale_linetype_manual(values = c('LN Conc' = NA, 
                                   'Linear (LN Conc)' = 'solid', 
                                   'LN Screening Level' = 'dashed')) +
  theme_classic() +
  theme(legend.position = "bottom", legend.box = "horizontal")

参数说明:

  • formula = y ~ x:指定线性回归公式(和geom_smooth一致)
  • parse = TRUE:让ggplot解析LaTeX格式的文本
  • position = position_nudge(x = 10):根据你的日期数据范围调整文本的水平位置,避免和图形元素重叠
  • size:设置文本大小

如果不想额外安装包,也可以手动计算回归结果后用annotate()添加:

# 手动计算回归
lm_model <- lm(LN_Conc ~ as.Date(Date), data = clean_1br)
eq_text <- paste0("y = ", round(coef(lm_model)[1], 2), " + ", round(coef(lm_model)[2], 6), "x")
r2_text <- paste0("R² = ", round(summary(lm_model)$r.squared, 4))

# 在图中添加文本
annotate("text", x = max(as.Date(clean_1br$Date)) - 15, y = 7.8, 
         label = paste(eq_text, r2_text, sep = "\n"), 
         color = "darkgrey", size = 4)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 19:07:11