Learn R Programming

itsadug (version 2.0)

plot_error: Utility function

Description

Plot line with confidence intervals.

Usage

plot_error(x, fit, se.fit, se.fit2 = NULL, shade = FALSE, f = 1,
  col = "black", ci.lty = NULL, ci.lwd = NULL, border = FALSE,
  alpha = 0.25, ...)

Arguments

x
Vector with values on x-axis.
fit
Vector with values on y-axis.
se.fit
Vector with standard error; or when se.fit2 is provided, se.fit specifies upper values confidence interval.
se.fit2
Optional: lower values confidence interval.
shade
Logical: whether or not to produce shaded regions as confidence bands.
f
Factor for converting standard error in confidence intervals. Defaults to 1. Use 1.96 for 95% CI, and 2.58 for 99% CI.
col
Color for lines and confindence bands.
ci.lty
Line type to be used for the error lines, see par.
ci.lwd
Line type to be used for the error lines, see par.
border
The color to draw the border for the shaded confidence interval. The default, FALSE, omits borders.
alpha
Transparency of shaded area. Number between 0 (completely transparent) and 1 (not transparent).
...
Optional arguments for the lines and shaded area.

See Also

Other Functions for plotting: addInterval, add_bars, alphaPalette, alpha, check_normaldist, color_contour, dotplot_error, drawArrows, emptyPlot, errorBars, fadeRug, fill_area, getCoords, getFigCoords, getProps, gradientLegend, marginDensityPlot, plot_image, plotsurface, rug_model

Examples

Run this code
data(simdat)

# Use aggregate to calculate mean and standard deviation per timestamp:
avg <- aggregate(simdat$Y, by=list(Time=simdat$Time),
    function(x){c(mean=mean(x), sd=sd(x))})
head(avg)
# Note that column x has two values in each row (a more intuitive way 
# to calculate different measures at the same time is implemented in 
# ddply (package plyr)):
head(avg$x)
head(avg$x[,1])

# Plot line and standard deviation:
emptyPlot(range(avg$Time), c(-20,20), h0=0)
plot_error(avg$Time, avg$x[,'mean'], avg$x[,'sd'])
# Change layout:
emptyPlot(range(avg$Time), c(-20,20), h0=0)
plot_error(avg$Time, avg$x[,'mean'], avg$x[,'sd'], 
   shade=TRUE, lty=3, lwd=3)

# Show difference with 0:
x <- find_difference(avg$x[,'mean'], avg$x[,'sd'], xVals=avg$Time)
# Add arrows:
abline(v=c(x$start, x$end), lty=3, col='red')
arrows(x0=x$start, x1=x$end, y0=-5, y1=-5, code=3, length=.1, col='red')

# Use of se.fit2 for asymmetrical error bars:
avg$cu <- avg$x[,'mean'] + .25*avg$x[,'sd']
avg$cl <- avg$x[,'mean'] - avg$x[,'sd']
emptyPlot(range(avg$Time), c(-20,20), h0=0)
plot_error(avg$Time, avg$x[,'mean'], se.fit=avg$cu, se.fit2=avg$cl, col='red')

# Some layout options:
emptyPlot(range(avg$Time), c(-20,20), h0=0)
plot_error(avg$Time, avg$x[,'mean'], avg$x[,'sd'], 
   lty=3, lwd=1, ci.lty=1, ci.lwd=3)
emptyPlot(range(avg$Time), c(-20,20), h0=0)
plot_error(avg$Time, avg$x[,'mean'], avg$x[,'sd'], 
   shade=TRUE, lty=3, lwd=3)
emptyPlot(range(avg$Time), c(-20,20), h0=0)
plot_error(avg$Time, avg$x[,'mean'], avg$x[,'sd'], 
   shade=TRUE, lty=1, lwd=3, ci.lwd=3, border='red')
plot_error(avg$Time, avg$x[,'mean'], avg$x[,'sd'], 
   shade=TRUE, lty=1, lwd=3, density=10, ci.lwd=3)

Run the code above in your browser using DataLab