errorBars: Add error bars to a plot.

Description Usage Arguments Author(s) See Also Examples

View source: R/plot.R

Description

Add vertical error bars.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
errorBars(
  x,
  mean,
  ci,
  ci.l = NULL,
  minmax = NULL,
  horiz = FALSE,
  border = FALSE,
  ...
)

Arguments

x

Vector with x-values (or y-values in case horiz=TRUE).

mean

Vector with means.

ci

Vector with errors or confidence bands, e.g. SE values. If ci.l is not defined, the errors are assumed to be symmetric. If ci.l is defined, than ci is assumed to be the upper confidence band. Note that the ci will be added (or substracted) from the mean.

ci.l

Optional: vector with error to calculate lower confidence band.

minmax

Optional argument, vector with two values indicating the minimum and maximum value for the error bars. If NULL (default) the error bars are not corrected.

horiz

Logical: whether or not to plot horizontal error bars. Defaults to FALSE (plotting vertical error bars).

border

Logical: whether or not to add a border around the error bars. Defaults to FALSE (no border added). Color and width of the borders can be adjusted using border.col and border.width. (Basically all parameters that work for errorBars, with border. added before.) See examples.

...

Optional graphical parameters (see par).

Author(s)

Jacolien van Rij

See Also

Other Functions for plotting: addInterval(), add_bars(), add_n_points(), alphaPalette(), alpha(), check_normaldist(), color_contour(), dotplot_error(), drawDevArrows(), emptyPlot(), fill_area(), getCoords(), getFigCoords(), getProps(), gradientLegend(), legend_margin(), marginDensityPlot(), plot_error(), plot_image(), plotsurface(), sortBoxplot()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# example InsectSprays from R datasets

InsectSprays$type <- ifelse( InsectSprays$spray %in% c('A', 'B', 'F'), 1,2)
avg <- with(InsectSprays, tapply(count, list(spray), mean))
sds <- with(InsectSprays, tapply(count, list(spray), sd))


# barplot:
b <- barplot(avg, beside=TRUE, main='Insect Sprays', ylim=c(0,20))
errorBars(b, avg, sds, xpd=TRUE, length=.05)

# constrain error bars to max and min of plot:
b <- barplot(avg, beside=TRUE, main='Insect Sprays', ylim=c(0,20))
errorBars(b, avg, sds, minmax=c(0,20), xpd=TRUE, length=.05)                

# add borders:
b <- barplot(avg, beside=TRUE, main='Insect Sprays', ylim=c(0,20),
     col=1, border=NA)
errorBars(b, avg, sds, minmax=c(0,20), xpd=TRUE, length=.05, border=TRUE) 

# change layout:
b <- barplot(avg, beside=TRUE, main='Insect Sprays', ylim=c(0,20),
     col=1, border=NA)
errorBars(b, avg, sds, minmax=c(0,20), xpd=TRUE, border=TRUE, 
     length=.05, col='blue', # settings for error bars 
     border.length=.1, border.col='yellow', border.lwd=5) # settings border

# line plot with asymmetric fake errors:
emptyPlot(toupper(letters[1:6]), 20, main='Averages', xlab='Spray')
ci.low <- abs(rnorm(6, mean=2))
ci.high <-  abs(rnorm(6, mean=4))

errorBars(1:6, avg, ci.high, ci.l= ci.low, length=.05, lwd=2)
points(1:6, avg, pch=21, type='o', lty=3, lwd=2,
    bg='white', xpd=TRUE)
# also horizontal bars possible:
errorBars(10, 1, 1.2, horiz=TRUE, col='red')

plotfunctions documentation built on April 28, 2020, 5:10 p.m.