Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: specifying color for different facets / panels in lattice

Tags:

graph

r

lattice

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

like image 810
jon Avatar asked Dec 15 '11 15:12

jon


1 Answers

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

like image 119
Ben Bolker Avatar answered Sep 21 '22 18:09

Ben Bolker