具有第二个y轴和第二个y轴的格子图?

时间:2016-07-01 17:43:35

标签: r lattice

我想将第二个y轴(右)和第二个x轴(顶部)添加到下面的(点阵)水平图中。这些轴应该只指示某些行和列(没有标签),因此模仿基本图形'地毯功能。如何才能做到这一点?

library(lattice)
library(latticeExtra)

## Generate a correlation matrix
d <- 50
L <- diag(1:d)
set.seed(271)
L[lower.tri(L)] <- runif(choose(d,2))
Sigma <- L %*% t(L)
P <- cor(Sigma)

## Panel function
my_panel <- function(...) {
    panel.levelplot(...)
    panel.abline(h = (1:4)*10, v = (1:4)*10, lty = 2)
    panel.axis(side = "top", at = (1:50)-0.5, draw.labels = FALSE) # maybe a panel axis could do it? why not centered?
}

## Plot
obj1 <- levelplot(P, xlab = "Column", ylab = "Row",
                  col.regions = grey(c(seq(1, 0, length.out = 600))),
                  panel = my_panel)
obj2 <- xyplot(NA~NA, ylim = c(0, 50),
               scales = list(x = list(at = (1:50)-0.5, labels = rep("", 50)),
                             y = list(at = (1:50)-0.5, labels = rep("", 50))))
doubleYScale(obj1, obj2, use.style = FALSE) # idea based on latticeExtra; only gives a 2nd y-axis, though

1 个答案:

答案 0 :(得分:2)

你对panel.rug()提出了一个好主意,但却被的默认裁剪所阻碍,该裁剪默认剪裁到了裁判的内部。要解决这个问题,您可以通过par.settings=参数关闭剪辑。如果要禁止在右侧和顶部面板边框上绘制默认轴刻度线,可以使用tck=参数执行此操作,如下所示。

my_panel <- function(...) {
    panel.levelplot(...)
    panel.abline(h = (1:4)*10, v = (1:4)*10, lty = 2)
    ## Have panel.rug print tick marks starting at 1 npc (edge of panel)
    ## and extending to 1.02 npc (slightly outside of panel). (See ?unit)
    panel.rug(x = (1:51)-0.5, y = (1:51)-0.5, 
              start = 1, end = 1.02,
              col="black")
}

levelplot(P, xlab = "Column", ylab = "Row",
          col.regions = grey(c(seq(1, 0, length.out = 600))),
          ## Suppress default scales on right and top sides, by setting their
          ## tick lengths to zero 
          scales = list(tck=c(1,0)),
          ## Turn off clipping, so that panel.rug can plot outside of the panel
          par.settings = list(clip = list(panel = "off")),          
          panel = my_panel)

enter image description here

相关问题