Re: [R] ow to apply a panel function to each of several data series plotted on the same graph in lattice

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Fri, 18 Jun 2010 04:18:06 -0700

Hi:

Here's a simple example:

a <- rnorm(100)
b <- rnorm(100, 2)
c <- rnorm(100, 5)
x <- 1:100

library(lattice)
xyplot(a + b + c ~ x, type = 'l')

HTH,
Dennis

On Fri, Jun 18, 2010 at 12:45 AM, David Wyllie <David.Wyllie_at_ndm.ox.ac.uk>wrote:

> Hi
>
> is it possible to fit a trend line (or some other panel function) through
> each of multiple data series plotted on the same graph? Specifically, while
> one can do something like
> xyplot(a+b+c~x)
> which plots three series, a,b & c, but can one automatically fit lines
> through each of them?
>
>
> I suppose one could generate three more variables afit, bfit, and cfit with
> a model & predict and then plot them, but wondered if there was an easier
> way.
>
>
> Thank you for any advice. Here is an example:
>
>
> # use an example panel function using smooth.spline; however, the issue
> relates to all panel functions
> # a panel function to fit smoothed lines through data
> panel.smooth.spline <- function(x, y,
> w=NULL, df, spar = NULL, cv = FALSE,
> lwd=plot.line$lwd, lty=plot.line$lty,col,
> col.line=plot.line$col,
> type, horizontal=FALSE,... )
> {
> # Deepayan Sarkar code from:
> http://www.mail-archive.com/r-help@r-project.org/msg39654.html
> x <- as.numeric(x)
> y <- as.numeric(y)
>
>
> ok <- is.finite(x) & is.finite(y)
>
>
> if (sum(ok) < 1)
> return()
> if (!missing(col)) {
> if (missing(col.line))
> col.line <- col
> }
> plot.line <- trellis.par.get("plot.line")
> if (horizontal) {
> spline <-
> smooth.spline(y[ok], x[ok],
> w=w, df=df, spar = spar, cv = cv, ...)
> panel.lines(x = spline$y, y = spline$x, col = col.line,
> lty = lty, lwd = lwd, ...)
> }
> else {
> spline <-
> smooth.spline(x[ok], y[ok],
> w=w, df=df, spar = spar, cv = cv, ...)
> panel.lines(x = spline$x, y = spline$y, col = col.line,
> lty = lty, lwd = lwd, ...)
> }
> }
>
>
>
>
> # a composite function combining the xyplot and smooth.spline functions
> panel.composite<-function(x,y,groups, subscripts, ...) {
> panel.xyplot(x,y,...)
> panel.smooth.spline(x,y,...)
> }
>
>
> # generate mock data; this comprises three data series, a,b,c with means of
> about 5,35, and 105.
> test <- data.frame(
> a = runif(100, min=0, max=10),
> b = 30+runif(100, min=0, max=10),
> c = 100+runif(100, min=0, max=10),
> x = 1:100)
>
>
> # illustrate data
> xyplot(a + b + c ~ x, data = test, type = "p", auto.key=TRUE)
>
>
> # plot with fits .. but actually only one fit
> xyplot(
> a + b + c ~ x,
> panel=panel.composite,
> data = test,
> type = "p",
> auto.key=TRUE)
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 18 Jun 2010 - 11:21:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 18 Jun 2010 - 12:00:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive