error_bars | R Documentation |
Adds 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,
...
)
pos |
Either a single value or a numeric vector of N values indicating the position(s) at which an error bar should be drawn. |
limits |
Either a vector of 2 values or a 2 x N matrix giving the lower and upper limits, respectively, of the error bars. |
lb |
A vector of N values with the lower limits for the error bars. |
ub |
A vector of N values with the upper limits for the error bars. |
arrow |
Logical; if |
flip |
Logical; if |
length |
The length of the arrowhead for the
call to |
code |
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 |
angle |
The angle of the lines creating the arrowhead
for the call to |
border |
The color to draw the border for
|
... |
Additional plotting parameters for
the |
# 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
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.