我想在图表中绘制回归的拟合回归线,其中还绘制了 -1 和 1 范围内的分箱变量。
lost <- ggplot(data = data_2,
aes(x=log_rank_ratio, y=quit, color = as.factor(favorite))) +
geom_point(size=2) +
theme_bw() +
xlab("Log Rank Ratio") +
ylab("Observed Likelihood of Quitting Mid-Match") +
geom_vline(xintercept=0, linetype="dashed", alpha=.7)
给出了下图(散点图中的x变量被分箱(data_2):
但是,我想要添加的是绘制以下回归的拟合线(data_1 中未分箱的 x 值):
fit1 <- lm(quit ~ favorite + log_rank_ratio + log_rank_ratio^2 + log_rank_ratio^3 , data = data_1)
如果您确实想拟合三次多项式,请使用
geom_smooth
与 method = lm
和 formula = y ~ poly(x, 3)
。您还需要将 data = data_1
传递给 geom_smooth
,如果您希望线条为黑色,请使用 color = "black"
outside aes
和 group = factor(favorite)
inside aes
:
library(ggplot2)
ggplot(data = data_2,
aes(x = log_rank_ratio, y = quit, color = as.factor(favorite))) +
geom_point(size = 2) +
geom_smooth(data = data_1, method = lm, formula = y ~ poly(x, 3),
se = FALSE, aes(group = as.factor(favorite)), color = "black") +
theme_bw() +
xlab("Log Rank Ratio") +
ylab("Observed Likelihood of Quitting Mid-Match") +
geom_vline(xintercept = 0, linetype = "dashed", alpha = 0.7)
创建于 2023-08-05,使用 reprex v2.0.2
使用的数据(从问题中大致推断)
set.seed(1)
data_2 <- data.frame(log_rank_ratio = c(seq(-2, -0.2, 0.2), seq(0.2, 2, 0.2)),
quit = c(189, 177, 191, 212, 209, 213, 225, 249.3, 249.2,
249.1, 249.7, 280, 275, 305, 281, 280, 305, 325,
326, 275)/1e4,
favorite = rep(0:1, each = 10))
data_1 <- data.frame(log_rank_ratio = rep(data_2$log_rank_ratio, each = 10),
quit = c(t(replicate(10, rnorm(20, data_2$quit, 0.005)))),
favorite = rep(0:1, each = 100))