Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

panel.smoother

Plot a smoothing line with standard error bounds.


Description

Plot a smoothing line with standard error bounds. This is based on the stat_smooth function from ggplot2.

Usage

panel.smoother(x, y, form = y ~ x, method = "loess", ...,
    se = TRUE, level = 0.95, n = 100,
    col = plot.line$col, col.se = col,
    lty = plot.line$lty, lwd = plot.line$lwd,
    alpha = plot.line$alpha, alpha.se = 0.25, border = NA,
    ## ignored: ##
    subscripts, group.number, group.value,
    type, col.line, col.symbol, fill,
    pch, cex, font, fontface, fontfamily)

Arguments

x, y

data points. If these are missing, they will be looked for in the environment of form. So in many cases you can skip these if passing form. In fact, for convenience, the formula can be passed as the first argument (i.e. x).

form, method

the smoothing model is constructed (approximately) as method(form, data = list(x=x, y=y), ...). See the Examples section for common choices.

...

further arguments passed on to the model function (method).

se, level

estimate standard errors on the smoother, at the given level, and plot these as a band.

n

number of equi-spaced points on which to evaluate the smooth function.

col, col.se, lty, lwd, alpha, alpha.se, border

graphical parameters. col and alpha apply to the smoothing line, while col.se and alpha.se apply to the shaded se region.

subscripts, group.number, group.value, type, col.line, col.symbol, fill, pch, cex, font, fontface, fontfamily

ignored.

Details

This should work with any model function that takes a formula argument, and has a predict method with a se argument.

Author(s)

Felix Andrews felix@nfrac.org

Based on stat_smooth by Hadley Wickham.

See Also

Examples

set.seed(1)
xy <- data.frame(x = runif(100),
                 y = rt(100, df = 5),
                 y2 = rt(100, df = 5) + 1)

xyplot(y ~ x, xy, panel = function(...) {
       panel.xyplot(...)
       panel.smoother(..., span = 0.9)
})

## per-group layers with glayer (pass `...` to get styles)
xyplot(y + y2 ~ x, xy) +
  glayer(panel.smoother(...))

## natural spline with 5 degrees of freedom
if (require("splines"))
  xyplot(y ~ x, xy) +
    layer(panel.smoother(y ~ ns(x,5), method = "lm"))

## thin plate regression spline with smoothness
## chosen by cross validation (see ?mgcv::gam)
if (require("mgcv"))
  xyplot(y ~ x, xy) +
    layer(panel.smoother(y ~ s(x), method = "gam"))

## simple linear regression with standard errors:
xyplot(y ~ x, xy) +
  layer(panel.smoother(x, y, method = "lm"), style = 2)

latticeExtra

Extra Graphical Utilities Based on Lattice

v0.6-29
GPL (>= 2)
Authors
Deepayan Sarkar <deepayan.sarkar@r-project.org>, Felix Andrews <felix@nfrac.org>
Initial release
2019-12-18

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.