我想在兩條虛線(-0.5 和 0.5)之間創建一個藍色陰影區域,嘗試使用 geom_polygon() 但沒有用。如何以最好的方式做到這一點?
model <- lm(Sepal.Width ~ Petal.Length, data = iris)
ggplot(data.frame(x = seq(model$residuals), y = model$residuals))
geom_point(aes(x, y))
geom_hline(yintercept = 0, linetype = "dashed")
geom_hline(yintercept = 0.5, linetype = "dotted")
geom_hline(yintercept = -0.5, linetype = "dotted")
labs(x = "Index", y = "Residuals",
title = paste("Residuals of", format(model$call)))

uj5u.com熱心網友回復:
您可以使用geom_ribbon
ggplot(data.frame(x = seq(model$residuals), y = model$residuals))
geom_point(aes(x, y))
geom_ribbon(aes(x, ymin = -0.5, ymax = 0.5), alpha = 0.3, fill = 'steelblue')
geom_hline(yintercept = 0, linetype = "dashed")
geom_hline(yintercept = 0.5, linetype = "dotted")
geom_hline(yintercept = -0.5, linetype = "dotted")
labs(x = "Index", y = "Residuals",
title = paste("Residuals of", format(model$call)))

uj5u.com熱心網友回復:
與annotate:
annotate("rect", xmin = -Inf, xmax = Inf, ymin = -0.5, ymax = 0.5, alpha = .2, fill = "blue")
輸出:
ggplot(data.frame(x = seq(model$residuals), y = model$residuals))
geom_point(aes(x, y))
geom_hline(yintercept = 0, linetype = "dashed")
geom_hline(yintercept = c(-0.5, 0.5), linetype = "dotted")
annotate("rect", xmin = -Inf, xmax = Inf, ymin = -0.5, ymax = 0.5, alpha = .2, fill = "blue")
labs(x = "Index", y = "Residuals",
title = paste("Residuals of", format(model$call)))

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/522770.html
標籤:rggplot2
