当超过两个调节变量时,xyplot不合并图

时间:2012-03-19 23:40:22

标签: r lattice

当我运行以下代码时,xyplot生成4个独立的图2到3个图, 而我想要一个4乘6格子(以节省房地产 轴上的空间anotation和legends)。

请注意,我的问题与此one的不同之处在于我不想这样做 看到四组轴/传说。

以下是一些示例数据:

B <- structure(list(yval = c(0.88, 4.31, 7.52, 3.21, 3.27, 4.93, 4.21, 
0.7, 0.68, 0.92, 3.86, 5.67, 9.08, 1.95, 3.27, 1.44, 2.38, 0.85, 
0.79, 0.55, 0.79, 10.52, 0.9, 4, 0.78, 2.46, 0.78, 1.64, 2.47, 
0.77, 0.83, 0.86, 3.65, 8.25, 0.65, 0.88, 0.95, 4.05, 4.98, 1.43, 
4.43, 2.94, 5.52, 0.9, 3.69, 0.79, 0.74, 1.49, 7.29, 0.58, 8.47, 
5.82, 0.84, 0.87, 0.69, 1.38, 0.83, 2.32, 0.86, 7.32, 6.73, 6.7, 
3.3, 1.58, 2.74, 0.88, 4.2, 3.79, 4.98, 2.54, 1.84, 1.2, 2.59, 
11.99, 0.78, 0.92, 0.59, 3.83, 0.92, 2.6, 0.95, 3.18, 2.75, 9.83, 
9.81, 0.55, 0.83, 6.29, 1.64, 1.12, 0.65, 3.96, 4.27, 3.99, 20, 
0.83, 6.23, 6.81, 0.86, 0.7), xval = c(0.62, 0.81, 9.01, 3.72, 
1.49, 3.92, 6.22, 6.64, 5.56, 6.64, 4, 7.36, 9.6, 1, 1.64, 3.34, 
3.47, 3.37, 4.34, 6.63, 7.62, 4.07, 5.69, 3.76, 9.74, 1.58, 1.53, 
2.62, 1.64, 1.18, 9.79, 9.9, 2.76, 7.96, 5.11, 4.74, 9.92, 0.49, 
9.05, 8.59, 0.7, 5.8, 5.34, 3.14, 6.96, 2.05, 8.29, 0.35, 7.52, 
6.56, 2.01, 7.92, 3.89, 6.31, 8.64, 6.18, 4.49, 0.63, 7.52, 7.82, 
1.25, 9.54, 4.68, 0.4, 1.38, 8.7, 4.71, 8.27, 5.72, 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.4, 0.2, 
0.4, 0.2, 0.2, 0.4, 0.4, 0.2, 0.2, 0.4, 0.2, 0.4, 0.4, 0.4, 0.4, 
0.2, 0.4, 0.4, 0.4, 0.4, 0.2, 0.4, 0.4, 0.2, 0.4, 0.4, 0.2, 0.2, 
0.2, 0.2, 0.2, 0.4, 0.4, 0.4, 0.2, 0.4, 0.4, 0.2, 0.2, 0.4, 0.4, 
0.2, 0.2, 0.2, 0.4, 0.2, 0.4, 0.4, 0.4, 0.4, 0.2, 0.2), p = c(4, 
12, 12, 8, 12, 8, 12, 4, 4, 8, 8, 4, 4, 8, 8, 8, 4, 12, 8, 4, 
12, 12, 12, 12, 8, 12, 4, 4, 8, 8, 8, 4, 8, 12, 4, 12, 12, 4, 
12, 8, 4, 4, 12, 4, 4, 8, 4, 4, 8, 4, 8, 12, 12, 8, 4, 8, 8, 
8, 8, 12, 4, 8, 4, 12, 4, 4, 12, 4, 12, 12, 8, 4, 4, 12, 8, 12, 
4, 4, 12, 4, 8, 4, 8, 12, 8, 4, 4, 4, 8, 4, 4, 12, 8, 12, 8, 
4, 4, 8, 8, 4), nsamp = c(100, 300, 300, 200, 300, 200, 300, 
100, 100, 200, 200, 100, 100, 200, 200, 200, 100, 300, 200, 100, 
300, 300, 300, 300, 200, 300, 100, 100, 200, 200, 200, 100, 200, 
300, 100, 300, 300, 100, 300, 200, 100, 100, 300, 100, 100, 200, 
100, 100, 200, 100, 200, 300, 300, 200, 100, 200, 200, 200, 200, 
300, 100, 200, 100, 300, 100, 100, 300, 100, 300, 300, 200, 100, 
100, 300, 200, 300, 100, 100, 300, 100, 200, 100, 200, 300, 200, 
100, 100, 100, 200, 100, 100, 300, 200, 300, 200, 100, 100, 200, 
200, 100)), .Names = c("yval", "xval", "gval", "type", "cr", 
"p", "nsamp"), row.names = c(NA, -100L), class = "data.frame")

这是我正在运行的代码:

library(lattice)
library(latticeExtra)
library(grid)

types<-rep(NA,6)
    types[1]<-expression(paste(epsilon,"=",0.2,", p=",4,sep=""))
    types[2]<-expression(paste(epsilon,"=",0.2,", p=",8,sep=""))
    types[3]<-expression(paste(epsilon,"=",0.2,", p=",12,sep=""))
    types[4]<-expression(paste(epsilon,"=",0.4,", p=",4,sep=""))
    types[5]<-expression(paste(epsilon,"=",0.4,", p=",8,sep=""))
    types[6]<-expression(paste(epsilon,"=",0.4,", p=",12,sep=""))
    types<-rep(types,4)
    cl<-rainbow(7)[-4]

xyplot(B$yval~B$xval|as.factor(B$p)*as.factor(B$cr)*as.factor(B$type),
    group=B$gval, as.table=TRUE, 
    ylab=expression(kappa(Sigma,S)), col=cl, xlab=expression(nu),  
    xlim=c(0,10), ylim=c(0,10), type=c("l","g"), lwd=5, cex.lab=2,
    strip=function(...){
        panel.fill(trellis.par.get("strip.background")$col[1])
        type <- types[panel.number()]
        grid::grid.text(label=type,x=0.5,y=0.5,gp=gpar(fontsize=20))
        grid::grid.rect()
    }, 
    key=list(text=list(c("A","B","C","D","E","F"),cex=2),  
    lines=list(type=rep("l",6), label.cex=2,col=cl,lwd=3),columns=3), 
    par.settings=list(par.xlab.text=list(cex=2),axis.text=list(cex=2),  
    par.ylab.text=list(cex=2)))

2 个答案:

答案 0 :(得分:2)

我认为您在调用xyplot的某个地方需要layout=c(6,4)。完成后,您将不得不重新配置许多其他设置。

答案 1 :(得分:2)

三个条件变量意味着它创建了一个三维网格的面板,其中第三个维度在多个页面上。一种替代方案是仅对两个变量进行调节;在这里,我使用:使第一个条件因子成为前两个原始条件因子的交集。

xyplot(B$yval~B$xval|as.factor(B$p):as.factor(B$cr)*as.factor(B$type), ...