我正在尝试学习如何突出显示和注释图表中的某些点。为了获得可重现的示例,我在 alr4 包中使用 UBSprices 数据集。
我正在绘制一条 ols 线和一条 y=x 线。我想突出显示并注释距离 OLS 线最远的点(即最高残差)。
这是我到目前为止的代码:
ggplot(UBSprices, aes(x = bigmac2003, y = bigmac2009)) + geom_point() + geom_smooth(method = "lm", se = FALSE) +
geom_abline(color = "green", size = 1) + coord_fixed()
最佳答案
您可以计算残差,然后识别那些绝对值大于某个截止分位数的残差。例如:
library(tidyverse)
library(alr4)
UBSprices %>%
mutate(resid = resid(lm(bigmac2009 ~ bigmac2003, data = .)),
mark = abs(resid) >= quantile(abs(resid), prob=0.9)) %>%
ggplot(aes(x = bigmac2003, y = bigmac2009)) +
geom_point(aes(colour=mark), show.legend=FALSE) +
geom_smooth(method = "lm", se = FALSE) +
geom_abline(color = "green", size = 1) +
coord_fixed() +
theme_bw() +
scale_colour_manual(values=c("blue", "red"))
关于r - 突出显示图中的最高残差 : R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63732527/