R: color for different facets / panels in the grid

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 in an alternate chart / panel - for example - it 2chr1is dark green, but chr10is considered purple. then again dark olive green and purple and so on.

(2) I want to use the reverse order of the graph, which 2chr9is below.

thank

enter image description here

+5
source share
1 answer

Use as.table=TRUEto change the order of panels and groups(together with extended colvec) 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)

layout .

enter image description here

, " " :

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

+6

All Articles