error_bars.RdAdds error bars to an existing plot.
error_bars(
pos,
limits = NULL,
lb = NULL,
ub = NULL,
arrow = TRUE,
flip = FALSE,
length = 0.05,
code = 3,
angle = 90,
border = NULL,
...
)Either a single value or a numeric vector of N values indicating the position(s) at which an error bar should be drawn.
Either a vector of 2 values or a 2 x N matrix giving the lower and upper limits, respectively, of the error bars.
A vector of N values with the lower limits for the error bars.
A vector of N values with the upper limits for the error bars.
Logical; if TRUE,
arrows are drawn at
each position, otherwise a call is made to
polygon to create a
filled-in segment to represent connected
error bars.
Logical; if TRUE, bars are
drawn horizontally instead of vertically.
In this case pos denotes the
position(s) on the y-axis. Otherwise,
pos denotes the position(s) on the
x-axis.
The length of the arrowhead for the
call to arrows.
Integer controlling whether to draw an
arrowhead at the start (1), end (2), or at both ends (3)
of the line for the call to arrows.
The angle of the lines creating the arrowhead
for the call to arrows. Using
90 degrees results in a flat bar per standard error bars.
The color to draw the border for
polygon.
Additional plotting parameters for
the arrows function,
or if arrow is FALSE,
polygon.
# Simulate 5 variables with increasing means
dtf <- lapply(lin(-.5, .5, 5), function(x) rnorm(100, mean = x))
names(dtf) <- paste0("V", 1:5)
dtf <- data.frame(dtf)
# Extract sample means and standard errors
sm <- data.frame(
M = colMeans(dtf),
SE = apply(dtf, 2, sem)
)
# Compute 95% confidence intervals around
# sample means
sm$LB <- sm$M + qnorm(.025) * sm$SE
sm$UB <- sm$M + qnorm(.975) * sm$SE
# Plot mean for each variable
plot(1:5, sm$M,
pch = 19,
xlab = "Variable", ylab = "Mean", ylim = c(-1, 1)
)
# Add error bars (as arrows) for 95% confidence intervals
error_bars(1:5, rbind(sm$LB, sm$UB))
# Add error bars (as connected filled segment)
error_bars(1:5, rbind(sm$LB, sm$UB),
arrow = FALSE, col = rgb(.5, .5, .5, .2), border = NA
)
# Histogram for draws from standard normal
set.seed(300)
x <- rnorm(1000)
hist(x,
col = "grey", border = "white", main = "",
xlab = "z-scores", freq = FALSE, xlim = c(-4, 4)
)
# Add horizontal bars showing difference between
# 95% and 68% coverage interval
error_bars(c(.1, .3),
lb = qnorm(c(.025, .16)), ub = qnorm(c(.975, .84)),
lwd = 2, flip = TRUE
)
# Label bars
text(c(-2.1, -1.1), c(.1, .3),
c("95%", "68%"),
pos = 2
)