提问人:anderwyang 提问时间:11/7/2023 更新时间:11/7/2023 访问量:22
在 ggplot2 中添加 lm() 结果中的数据失败的 abline
Add abline in ggplot2 which data from lm() result failed
问:
我想在散点图中添加abline(),吹代码只能返回散点图(abline不能显示)。有人可以帮忙吗?谢谢!
library(tidyverse)
model <- lm(mpg~ disp,data=mtcars)
abline_data <- data.frame(intercept=coef(model)[1],
slope=coef(model)[2])
mtcars %>% ggplot(aes(x=mpg,y=disp))+
geom_point()+
geom_abline(data=abline_data,aes(intercept=coef(model)[1] ,slope=coef(model)[2]),
color="red",size =6)
答:
1赞
Andy Baxter
11/7/2023
#1
在调用中切换自变量/因变量以匹配函数(与 y 轴相关,与 x 轴无关):ggplot(aes())
mpg
disp
library(tidyverse)
model <- lm(mpg~ disp,data=mtcars)
abline_data <- data.frame(intercept=coef(model)[1],
slope=coef(model)[2])
mtcars %>% ggplot(aes(x=disp,y=mpg))+
geom_point()+
geom_abline(data=abline_data,
aes(intercept=intercept,
slope=slope),
color="red",size =6)
评论
aes(intercept = intercept, slope = slope)