【发布时间】:2021-12-20 10:26:16
【问题描述】:
在 R 统计包中,有没有办法用一个连续变量和一个分类变量绘制二阶多项式回归图?
要生成具有一个分类变量的线性回归图:
library(ggplot2)
library(ggthemes) ## theme_few()
set.seed(1)
df <- data.frame(minutes = runif(60, 5, 15), endtime=60, category="a")
df$category = df$category=letters[seq( from = 1, to = 2 )]
df$endtime = df$endtime + df$minutes^3/180 + df$minutes*runif(60, 1, 2)
ggplot(df, aes(y=endtime, x=minutes, col = category)) +
geom_point() +
geom_smooth(method=lm) +
theme_few()
绘制具有一个连续变量的多项式图:
ggplot(df, aes(x=minutes, y=endtime)) +
geom_point() +
stat_smooth(method='lm', formula = y ~ poly(x,2), size = 1) +
xlab('Minutes of warm up') +
ylab('End time')
但我不知道如何绘制一个包含一个连续变量和一个分类变量的多项式图。
【问题讨论】:
标签: r ggplot2 regression