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

ggplot2散点图添加平滑回归直线失败,该如何正确实现?

问题与解决:为横向散点图添加平滑回归直线

问题描述

尝试使用以下R代码为散点图添加平滑回归直线,但生成的图像中未显示回归直线:

lasso.model.bin.df.nozero %>%
  mutate(variable = fct_reorder(variable, lasso.coef)) %>%
  ggplot(aes(lasso.coef, variable, color = color)) +
  geom_vline(xintercept = 0, linetype = 2) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", se = TRUE) +
  scale_color_identity() +
  scale_x_continuous(breaks=c(-3, 0, 3, 6), 
                      limits=c(-3,6)) +
  xlab(paste("Aggregate binomial regression estimate",
             "(14th CE-18th CE)", sep = "\n")) +
  theme_bw(base_size = 16) +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(linetype = 2))

生成的图像仅显示散点和竖线,无回归直线。

可复现数据:

lasso.model.bin.df.nozero <- structure(list(verb = c("ThemeDef.bin", "ThemeAnimacy.bin", 
"LenDiff", "chuanshou", "ci", "dai", "di", "fa.1", "fen", "fu", "gei", "huan.1", 
"ji", "jia", "jiang.1", "jiao.1", "juan", "ke", "liu", "mai.1", 
"mai.2", "pei", "qi", "rang", "shang", "shao", "she", "shu", 
"song", "ti", "tiao", "xian", "xie", "yao", "zhao"), lasso.coef = c(2.42343012998206, 
1.50992004346556, -0.260489747921125, -0.437974835187702, -1.43186775058091, 
2.45953289423522, -1.00332755937803, -1.76710253151868, 0.0159421672397665, 
-1.56095215723717, -2.40012976566433, 4.28392746224767, -2.96910403951047, 
-1.96349687513487, 5.83503207861952, -1.654161123018, 2.47749182388819, 
4.02351393169698, 1.66385027569787, 1.53630222452288, -0.5057862134867, 
5.1687920938273, 3.76269176532588, -1.76637023953311, -0.506162662311249, 
0.10462960268202, -1.90739588803801, -2.25102305327462, 1.05244125756794, 
-0.716005133024394, -0.97649586552789, -2.52457183130426, -0.200876155316301, 
1.11320557096669, 3.74031197421905), epoch.estimate.num = c(0.00158580281442652, 
0.000636692078700501, 0.000108462531421402, 9.08252501762538e-05, 
3.36193443720819e-05, 0.00164400486304142, 5.16052716573677e-05, 
2.40437820754568e-05, 0.00014299392691628, 2.9548154585438e-05, 
1.27669584768092e-05, 0.0101048842420082, 7.22748941329972e-06, 
1.97565753538538e-05, 0.0459362695115951, 2.69185315665559e-05, 
0.00167374627671508, 0.00780625059412536, 0.00074256501712568, 
0.000653701814338089, 8.48709535307841e-05, 0.0241337216285987, 
0.00602487292456066, 2.40613951688994e-05, 8.48390126830835e-05, 
0.000156252975971102, 2.08965946920693e-05, 1.48198148117073e-05, 
0.000403042143666465, 6.87811056923123e-05, 5.30085977250099e-05, 
1.12731060412884e-05, 0.000115124219042401, 0.000428281288183683, 
0.00589232079734365), rank = 1:35, color = c("forestgreen", "forestgreen", 
"red", "red", "red", "forestgreen", "red", "red", "forestgreen", 
"red", "red", "forestgreen", "red", "red", "forestgreen", "red", 
"forestgreen", "forestgreen", "forestgreen", "forestgreen", "red", 
"forestgreen", "forestgreen", "red", "red", "forestgreen", "red", 
"red", "forestgreen", "red", "red", "red", "red", "forestgreen", 
"forestgreen")), row.names = c(NA, -35L), class = "data.frame")

原因分析

你的图是横向散点图:x轴是连续变量lasso.coef,y轴是分类变量(经fct_reorder处理后的因子)。geom_smooth(method="lm")默认拟合公式为y ~ x,但分类变量无法作为线性回归的因变量,因此无法生成回归直线。

另外原代码存在小问题:数据中没有variable列,应该将verb列重排序后命名为variable。

解决方法

方法1:指定回归公式(保持横向布局)

使用stat_smooth并明确指定公式x ~ y,让连续变量作为因变量,分类变量的数值化版本(对应因子的顺序)作为自变量,即可拟合横向的回归直线:

library(tidyverse)

lasso.model.bin.df.nozero %>%
  mutate(variable = fct_reorder(verb, lasso.coef)) %>% # 修正variable列的定义
  ggplot(aes(lasso.coef, variable, color = color)) +
  geom_vline(xintercept = 0, linetype = 2) +
  geom_point(size = 2) +
  stat_smooth(method = "lm", se = TRUE, formula = x ~ y) + # 关键:指定x~y的回归公式
  scale_color_identity() +
  scale_x_continuous(breaks=c(-3, 0, 3, 6), 
                      limits=c(-3,6)) +
  xlab(paste("Aggregate binomial regression estimate",
             "(14th CE-18th CE)", sep = "\n")) +
  theme_bw(base_size = 16) +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(linetype = 2))

方法2:互换坐标轴(纵向布局,更符合常规回归图逻辑)

将x和y轴互换,让连续变量作为y轴,分类变量作为x轴,此时geom_smooth默认的y ~ x回归可正常运行。如果仍需要横向布局,可添加coord_flip():

lasso.model.bin.df.nozero %>%
  mutate(variable = fct_reorder(verb, lasso.coef)) %>%
  ggplot(aes(variable, lasso.coef, color = color)) +
  geom_hline(yintercept = 0, linetype = 2) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", se = TRUE) +
  scale_color_identity() +
  scale_y_continuous(breaks=c(-3, 0, 3, 6), 
                      limits=c(-3,6)) +
  ylab(paste("Aggregate binomial regression estimate",
             "(14th CE-18th CE)", sep = "\n")) +
  theme_bw(base_size = 16) +
  theme(panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.x = element_line(linetype = 2)) +
  coord_flip() # 可选:保持横向布局

内容的提问来源于stack exchange,提问作者nick.yili9393

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 14:42:04