Skip to content
Prev 244621 / 398502 Next

overlap different line in a xyplot (lattice)

On 2010-12-11 03:12, Francesco Nutini wrote:
The idea is the same: you need to get your data
into "long" format with a grouping variable and
then use the 'groups' argument to xyplot.
Here's fake data frame (you should have provided one):

  DF <- data.frame(y1 = rnorm(30),
                   y2 = rnorm(30) + 2,
                   x  = rep(1:10, 3),
                sites = gl(3, 10, lab=LETTERS[1:3]))

## Use the reshape2 package to melt the data:
## (or use reshape() in base R)
  require(reshape2)
  DF1 <- melt(DF, measure.vars = c('y1', 'y2'),
              variable.name = 'grp', value.name = 'y')

## and plot:
  require(lattice)
  p <- xyplot( y ~ x | sites, data = DF1, groups = grp,
              col = c("red", "blue"), type = "b")
  print(p)

Peter Ehlers