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