我想使用ggplot2注释所有大于y阈值的y值。
当您使用基本程序包plot(lm(y~x))
时,第二个自动弹出的图形是Residuals vs Fitted,第三个是qqplot,第四个是Scale-location。这些中的每一个都通过将其对应的X值作为相邻注释列出来自动标记您的极端Y值。我正在寻找这样的东西。
使用ggplot2实现此基本默认行为的最佳方法是什么?
最佳答案
更新了 scale_size_area()
代替scale_area()
您也许可以从中获得一些满足您需求的东西。
library(ggplot2)
#Some data
df <- data.frame(x = round(runif(100), 2), y = round(runif(100), 2))
m1 <- lm(y ~ x, data = df)
df.fortified = fortify(m1)
names(df.fortified) # Names for the variables containing residuals and derived qquantities
# Select extreme values
df.fortified$extreme = ifelse(abs(df.fortified$`.stdresid`) > 1.5, 1, 0)
# Based on examples on page 173 in Wickham's ggplot2 book
plot = ggplot(data = df.fortified, aes(x = x, y = .stdresid)) +
geom_point() +
geom_text(data = df.fortified[df.fortified$extreme == 1, ],
aes(label = x, x = x, y = .stdresid), size = 3, hjust = -.3)
plot
plot1 = ggplot(data = df.fortified, aes(x = .fitted, y = .resid)) +
geom_point() + geom_smooth(se = F)
plot2 = ggplot(data = df.fortified, aes(x = .fitted, y = .resid, size = .cooksd)) +
geom_point() + scale_size_area("Cook's distance") + geom_smooth(se = FALSE, show_guide = FALSE)
library(gridExtra)
grid.arrange(plot1, plot2)
关于r - 用ggplot标记/注释极值的最简洁方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10310728/