Confidence bands with lattice and R

[This article was first published on Omnia sunt Communia! » R-english, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you use lattice with R, and you need to plot confidence limits in your graphic, then panel.smoother and panel.quantile from latticeExtra will help you with this task. These functions internally calculate the error bounds and use panel.polygon from lattice. If you need to plot your own confidence limits, then you have to define a panel function.

A suitable code is proposed here by Deepayan Sarkar, the developer of lattice:

my.panel.bands <- function(x, y, upper, lower, fill, col,
 subscripts, ..., font, fontface)
 upper <- upper[subscripts]
 lower <- lower[subscripts]
 panel.polygon(c(x, rev(x)), c(upper, rev(lower)),
 col = fill, border = FALSE,

Then you can plot your data.frame, named “data” with:

xyplot(formula, data=data, groups=groups,
 upper = data$high, lower = data$low,
 panel = function(x, y, ...){
 panel.superpose(x, y, panel.groups = my.panel.bands, type='l', col='gray',...)
 panel.xyplot(x, y, type='b', cex=0.6, lty=1,...)

where “data$high” and “data$low” are the error bounds of your data. You can also use the glayer function from latticeExtra if you do not feel confortable with panel functions inside xyplot.

In this article, I have used this tool to display confidence limits of the wavelet variance calculated with wmtsa, and I got the next figure:

You will find here the R code used for this article.

To leave a comment for the author, please follow the link and comment on their blog: Omnia sunt Communia! » R-english. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)