作为第一个示例(点不可能直接位于回归线上):计算每个 x 值的拟合值,在实际绘图之前拟合线性模型。之后,您可以简单地将布尔列绑定到数据框,指定预测值是小于还是大于真实 y。
library(ggplot2)
rowid <- (1:1000)
intercept <- rnorm(1000, 0, 1)*rowid+1:1000
mod <- lm(intercept ~ rowid)
fitted <- mod$fitted.values
smaller <- intercept < fitted
df <- data.frame(rowid, intercept, smaller)
ggplot(df, aes(x=rowid, y=intercept, color=as.factor(smaller))) +
geom_point()+
geom_smooth(method='lm', color = 'black')+
scale_color_manual(values = c("green", "yellow"))
现在才阅读了您帖子的最后一句话,一种引入具有“A”、“F”和“C”级别的三级因子列并在之后相应地绘制它们的方法。我介绍了 200 个点,其中拟合值和真 y 相等:
library(ggplot2)
rowid <- (1:1000)
intercept <- rnorm(1000, 0, 1)*rowid+1:1000
mod <- lm(intercept ~ rowid)
fitted <- mod$fitted.values
#demonstration: add some points, where fitted value equals the true value
sample_points <- sample(1:1000, 200, replace = FALSE)
intercept[sample_points] <- fitted[sample_points]
#construct three level factor, with a chain of ifelse:
grade <- as.factor(ifelse(intercept < fitted, "F", ifelse(intercept == fitted, "C", "A")))
df <- data.frame(rowid, intercept, grade)
ggplot(df, aes(x=rowid, y=intercept, color=grade)) +
geom_point()+
geom_smooth(method='lm', color = 'black')+
scale_color_manual(values = c("green", "yellow", "red"))