我有一个散点图和基础回归模型。我想在另一个数据点上给出一个很好的例子(假设不包含在估计样本中),即它的实际值与预测值。除了标签之外,我已经准备好了一切(考虑geom_text或geom_label,到目前为止没有任何效果):
data(mtcars)
model <- lm(mpg ~ wt, data=mtcars)
mycar <- data.frame(wt=c(2.5))
predict(model, mycar)
model <- lm(Coupon ~ Total, data=df)
mycar <- data.frame(Total=c(79037022, 83100656, 104299800))
predict(model, new.df)
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_smooth(method="lm", se=FALSE) +
geom_point(aes(x=2.5,y=23.92395), # red is the prediction for my car
color='red',
size=3, show.legend = TRUE) +
geom_point(aes(x=2.5,y=28), # green is the actual mpg of my car
color='green',
size=3)
Run Code Online (Sandbox Code Playgroud)
我对具体实施很灵活:
我正在寻找易于实施且具有视觉吸引力的东西。谢谢
我推荐该ggrepel包提供一些功能,以避免与绘图上已绘制的现有项目重叠,特别是当您有多个标签/文本时
library(ggplot2)
library(ggrepel)
data(mtcars)
model <- lm(mpg ~ wt, data = mtcars)
mycar <- data.frame(wt = c(2.5))
ggplot() +
geom_point(data = mtcars, aes(x = wt, y = mpg)) +
geom_smooth(data = mtcars, aes(x = wt, y = mpg), method = "lm", se = FALSE) +
geom_point(aes(x = 2.5, y = 23.92395), # red is the prediction for my car
color = "red",
size = 3, show.legend = TRUE
) +
geom_point(aes(x = 2.5, y = 28), # green is the actual mpg of my car
color = "green",
size = 3
) +
geom_label_repel(aes(x = 2.5, y = 23.92395),
# using nudge_x & nudge_y to manual adjust the location to avoid
# label overlay on a data point around 3.5 & 25
color = "red", label = "My car prediction", nudge_x = .5, nudge_y = 1.5
) +
geom_label_repel(aes(x = 2.5, y = 28),
color = "green", label = "My actual car", nudge_x = .5, nudge_y = 1
)
#> `geom_smooth()` using formula 'y ~ x'
Run Code Online (Sandbox Code Playgroud)

ggplot() +
geom_point(data = mtcars, aes(x = wt, y = mpg)) +
geom_smooth(data = mtcars, aes(x = wt, y = mpg), method = "lm", se = FALSE) +
geom_point(aes(x = 2.5, y = 23.92395), # red is the prediction for my car
color = "red",
size = 3, show.legend = TRUE
) +
geom_point(aes(x = 2.5, y = 28), # green is the actual mpg of my car
color = "green",
size = 3
) +
geom_text_repel(aes(x = 2.5, y = 23.92395),
color = "red", label = "My car prediction", nudge_x = .5, nudge_y = 1
) +
geom_text_repel(aes(x = 2.5, y = 28),
color = "green", label = "My actual car", nudge_x = .5, nudge_y = 1
)
#> `geom_smooth()` using formula 'y ~ x'
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v2.0.0)创建于 2021-05-04