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

为何使用lm方法调用geom_smooth时无法关闭se绘图?

解决geom_smooth(method='lm')中se=FALSE不生效的问题

别担心,这个问题其实挺常见的——大概率是你把se=FALSE参数放错位置啦!

先回顾下你能正常运行的代码:

library(tidyverse)
x <- seq(1, 10)
a <- 2 * x
b <- 3 * x
df <- data.frame(cbind(x, a, b)) %>% gather(class, y, a:b)
ggplot(df, aes(x = x, y = y, color = class)) + geom_point() + geom_smooth(method = 'lm')

要关闭拟合线的置信区间(也就是se),必须把se=FALSE直接放到geom_smooth()函数的参数列表里,而不是放在ggplot()或者其他图层函数里。比如如果错误地写成ggplot(..., se=FALSE)或者geom_point(se=FALSE),肯定不会生效。

正确的写法应该是这样:

ggplot(df, aes(x = x, y = y, color = class)) + 
  geom_point() + 
  geom_smooth(method = 'lm', se = FALSE)  # 把se=FALSE放在这里才对!

背后的逻辑很简单:se是geom_smooth()的专属参数,专门用来控制是否显示拟合线的置信区间。只有把它传递给这个函数,ggplot才能识别并执行关闭置信区间的操作。

如果确认参数位置没问题但还是不生效,那可以试试更新你的tidyverse包,旧版本偶尔会有小bug:

install.packages("tidyverse")

一般来说,只要参数位置正确,这个问题就能解决啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:28:49