【问题标题】:xyplot not merging plots when more than two conditioning variables当超过两个条件变量时,xyplot 不合并图
【发布时间】:2012-03-20 12:35:18
【问题描述】:

当我运行以下代码时,xyplot 会生成 4 个单独的图 2 x 3 的图, 而我想要一个 4 x 6 的格子(以节省房地产 轴注释和图例上的空格)。

请注意,我的问题与 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.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, 0.4, 0.2, 0.2, 0.4, 0.2, 0.2, 0.2, 0.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)))

【问题讨论】:

  • user189035 -- 请点击“编辑”按钮查看我是如何清理您的代码的,以使其显示更具可读性。如果你在未来的问题上这样做,它会让人们更容易帮助你。谢谢。
  • 谢谢,我以后会这样做(似乎主stackoverflow上的代码模式与姐妹网站有点不同)。

标签: r lattice


【解决方案1】:

三个条件变量意味着它制作了一个三维网格的面板,其中第三个维度位于多个页面上。一种替代方法是仅以两个变量为条件;这里我使用:使第一个条件因子成为前两个原始条件因子的交集。

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

【讨论】:

  • 两种解决方案都有效,但这个解决方案需要较少的代码调整。
【解决方案2】:

我认为您在调用 xyplot 时需要 layout=c(6,4)。一旦你这样做了,你将不得不重新配置许多其他设置。

【讨论】:

  • +1 -- 这是正确的想法。鉴于条件变量的顺序和面板上的标签,我认为它实际上应该是layout = c(6,4)。此外(正如我认为 Seth 所暗示的那样)您可能需要更改您的 xlabylab
猜你喜欢
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
  • 2012-09-23
  • 2023-03-19
  • 2016-12-19
相关资源
最近更新 更多