Skip to content
Prev 168963 / 398506 Next

xyplot with lowess curves

I haven't had much luck with a custom panel function; mainly because I
don't truly understand how to embedd the functionality into the xyplot
command.

Here's a reproducible example if you can help out.

Thanks,
Dave

library (lattice)

d <- NULL
models <- c('A','B','C','D')
n = 100
for (i in seq(along = models)){
  d <- rbind(
    d, data.frame (
      Model = models[i],
      Year = seq(1960, length.out=n, by = 1),
      SnowLineElevation = runif(n, 0, 100)
    )
  )
}

xyplot (
  SnowLineElevation ~ Year | Model,
  data = d,
  ylim = c(0,100),
  type = c('p','smooth'),
  col = 'blue',
  pch = 21,
  xlab = 'Year',
  ylab = 'Snowline Elevation [m]'
) 

-----Original Message-----
From: Sundar Dorai-Raj [mailto:sdorairaj at gmail.com] 
Sent: Monday, February 02, 2009 11:43 AM
To: Hutchinson,David [PYR]
Cc: r-help at r-project.org
Subject: Re: [R] xyplot with lowess curves

You'll need a custom panel function. It would also help if you provided
a reproducible example:

xyplot (
  SnowLineElevation ~ Year | Model,
  data = data,
  panel = function(x, y, col, ...) {
    col <- ifelse(panel.number() == 1, "red", "green")
    panel.xyplot(x, y, col = "blue", ...)
    panel.loess(x, y, col = col)
  },
  ylim = c(0,1800),
  pch = 21,
  xlab = 'Year',
  ylab = 'Snowline Elevation [m]'
)

Alternatively, you can use the group argument in conjunction with the
panels:

xyplot(SnowLineElevation ~ Year | Model, data, groups = Model, type =
c("p", "smooth"))

if you want the points and the lines to be the same color.

--sundar

On Mon, Feb 2, 2009 at 10:20 AM, Hutchinson,David [PYR]
<David.Hutchinson at ec.gc.ca> wrote: