5

My data is as follows:

grp = rep(1:2, each = 100)
chr = c(rep(1:10, each = 10), rep(1:10, each = 10))
var = paste (grp, "chr", chr, sep = "")
pos = (rep(1:10, 20)) 
yvar = rnorm(200) 
mydf = data.frame (var, pos, yvar)

require( lattice)
xyplot(yvar ~ pos| factor(var), data = mydf, layout = c(1,10), type = c("g", "h"),
         col = "darkolivegreen", lwd = 4)

(1) I want to put different colors to alternate graph / panel - for example - 2chr1 is darkolive green but chr10 is say purple. then again dark olive green and purple so on.

(2) I want to use reverse order of graph means that 2chr9 is at the bottom.

Thanks

enter image description here

jon
  • 11,186
  • 19
  • 80
  • 132

1 Answers1

6

Use as.table=TRUE to change the order of panels and groups (along with an extended col vec) to change colo(u)rs.

edit: adjusted order of factor levels

mydf <- 
  data.frame (var, pos, yvar, 
              ##  fvar = factor(var,levels=unique(var)),
              fvar = factor(var, levels = c(outer(2:1, 1:10, paste, sep="chr"))))

xyplot(yvar ~ pos| fvar,
       groups=fvar,
       data = mydf, layout = c(1,10,2), type = c("g", "h"),
       col = c("darkolivegreen","purple"), lwd = 4, as.table=TRUE)

The extended layout command gives two pages.

enter image description here

Alternatively, a side-by-side layout might be nice:

library(latticeExtra)
useOuterStrips(xyplot(yvar ~ pos|factor(grp)*factor(chr),
                      groups=grp,
                      col=c("darkolivegreen","purple"),
                      data = mydf, layout = c(2,10), type = c("g", "h"),
                      lwd = 4, as.table=TRUE))

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • @ Ben, thanks, AS.TABLE works but the col doesnot produce what I intend to do. I did not want to put different color to alternative values rather to alternate panel ... – jon Dec 15 '11 at 15:22
  • ?? I thought that's what my solution did. Is the picture (just added) not what you wanted? (Did you forget the `groups` specification?) – Ben Bolker Dec 15 '11 at 15:24
  • The sensible layout I want is 1Chr1 follows 2chr2, 1chr2, follow 2chr2....and so on .... – jon Dec 15 '11 at 15:38
  • The comment doesn't make perfect sense to me. I interpret it as 2chr2, 1chr1, 2chr2, 1chr2 ... maybe you meant 2chr1, 1chr1, 2chr2, 1chr2 ... (more sensible but I still don't see why 2chr* would precede 1chr* – Ben Bolker Dec 15 '11 at 15:42
  • Each grp = 1:2, should be grouped for each level of chr, although myintension was to combined them to make a single name like var = paste (grp, "chr", chr, sep = "") – jon Dec 15 '11 at 15:47
  • See revised code above (you can change `2:1` to `1:2` in the `outer` statement if that order makes more sense) – Ben Bolker Dec 15 '11 at 15:54