繁体   English   中英

具有二元密度叠加等高线图的散点图矩阵卡在晶格r中

[英]scatterplot matrix with overlaid contourplot of bivariate density gets stuck in lattice r

我正在使用lattice创建具有双变量核密度函数的叠加轮廓图的散点图矩阵。 以下代码给出了一种奇怪的行为,即轮廓图仅从底部开始到顶部被部分切除,仅部分绘制。 抽取多少取决于MASS::kde2dn的值。

library(lattice)
library(MASS)

splom(iris, upper.panel = function(x, y, ...) {
  if(is.numeric(x) & is.numeric(y)){

    # calculate bivariate kernel density 
    f1 <- kde2d(x = x, y = y, n = 20) #, lims = c(0, 10 ,0, 10))

    f <- data.frame(x = f1$x, y = rep(f1$y, each = length(f1$x)), 
                    z = as.vector(f1$z))

    panel.contourplot(x = f$x, y = f$y, z = f$z,  
                      contour = TRUE, ...)
    }
    panel.xyplot(x, y, ...)
  })

浏览并打印中间值的摘要似乎表明这些函数的行为符合预期,并给出了预期范围内的值。 知道发生了什么吗?

好的,事实证明...正在将旧的下标参数传递给新的面板函数。 由于iris数据具有25 * 25 = 125个下标,因此panel.contourplot仅考虑其xyz参数的前125个元素。 下面解决这个问题。

splom(iris, upper.panel = function(x, y, subscripts, ...) {
  if(is.numeric(x) & is.numeric(y)){

    # calculate bivariate kernel density 
    v <- current.panel.limits() # allows full bleed by setting limits explicitly
    f1 <- kde2d(x = x, y = y, n = 50, lims = c(v$xlim, v$ylim))

    f <- data.frame(x = f1$x, y = rep(f1$y, each = length(f1$x)), 
                    z = as.vector(f1$z))
    panel.contourplot(f$x, f$y, f$z, contour = TRUE, 
                      subscripts = 1:dim(f)[1], ...)
  }
  panel.xyplot(x, y, subscripts = subscripts, ...)
})

在进行此操作时,我添加了一些代码以使levelplot占据整个面板,而不是在边缘周围levelplot讨厌的白色边框。 好多了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM