R:为格子中的不同面/面板指定颜色

时间:2011-12-15 15:04:37

标签: r graph lattice

我的数据如下:

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)我想为备用图形/面板添加不同的颜色 - 例如 - 2chr1是暗绿色但chr10是紫色。然后又是深橄榄绿和紫色等等。

(2)我想使用图的反向顺序意味着2chr9位于底部。

谢谢

enter image description here

1 个答案:

答案 0 :(得分:6)

使用as.table=TRUE更改面板的顺序和groups(以及扩展的col vec)以更改colo(u)rs。

修改:调整因子级别的顺序

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