A slight modification to polygon
with quality of life improvements to make it easier to add colored confidence
intervals to a plot. Default values are taken for all
polygon
arguments, except for border
, which
is set to NA
. Additionally, an alpha
argument has been added to
accommodate partially transparent confidence intervals. Any of the input values
can be overwritten within this function's call.
ribbon(
x,
y,
density = NULL,
angle = 45,
border = NA,
col = NA,
lty = par("lty"),
...,
fillOddEven = FALSE,
alpha = NULL
)
If y
is a two column data.frame or matrix, ribbon
will convert y
to a vector
such that y = c(y[,1], rev(y[,2]))
in order to create the lower and upper bounds of
the polygon. Additionally, when y
is a two column data.frame or matrix, x
can have the same
length as the number of rows in y
, and ribbon
will concatenate the reverse of the
vector x
to ensure it has equal length.
if (FALSE) {
# Load data
data(cars)
# fit model
m1 <- lm(
dist ~ speed,
data = cars
)
# make predictions
preds <- predict(
m1,
newdata = data.frame(speed = 10:25),
interval = "confidence"
)
# base plot
blank(
xlim = c(10,25),
ylim = c(15,120),
xlab = "Speed",
ylab = "Stopping distance",
xaxt = "s",
yaxt = "s",
bty = "l",
las = 1
)
# add 95% confidence interval
ribbon(
x=10:25,
y=preds[,c("lwr","upr")],
col = "purple",
alpha = 0.5
)
# add mean prediction
lines(
x=10:25,
y = preds[,"fit"],
lwd =2,
col = "purple"
)
# add data
points(
x = cars$speed,
y = cars$dist,
pch = 16
)
}