简体   繁体   English

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

[英]Lattice plot with both a second x- and a second y-axis?

I would like to add a 2nd y-axis (right) and a 2nd x-axis (top) to the following (lattice) levelplot. 我想将第二个y轴(右侧)和第二个x轴(顶部)添加到以下(晶格)水平图中。 These axes should only indicate certain rows and columns (no labels) and thus mimick base-graphics' rug function. 这些轴应仅指示特定的行和列(无标签),从而模仿基本图形的rug功能。 How can this be done? 如何才能做到这一点?

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

You were onto a good idea with panel.rug() , but were stymied by lattice 's default clipping of its plotting to the panel's interior. 您对panel.rug()有了一个不错的主意,但是格子的默认剪裁(其对面板内部的绘图的默认剪裁panel.rug() To get around that, you can turn off clipping via the par.settings= argument. 为了解决这个问题,您可以通过par.settings=参数关闭裁剪。 If you want to suppress the plotting of default axis tick marks on the right and top panel borders, you can do so using the tck= argument, as shown below. 如果要禁止在右侧和顶部面板边框上绘制默认的轴刻度线,则可以使用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)

在此处输入图片说明

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

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