【问题标题】:Lattice plot with both a second x- and a second y-axis?带有第二个 x 轴和第二个 y 轴的格子图?
【发布时间】:2016-07-06 04:26:58
【问题描述】:

我想将第二个 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

【问题讨论】:

  • 您能否更清楚地说明所需的输出是什么?也许创建一个草图来展示您正在尝试创建的内容?第二个轴应该有不同的比例吗?你只是想给事物贴标签吗?
  • 我试图在绘制相关矩阵时指示块矩阵中的某些行和列。这些块对应于数据的行业部门和子部门。上述通过 panel.abline() 的方法适用于部门,但子部门太多了。与 rug() 类似,我想在顶部和右侧(第二轴)指示外部子扇区的行和列。
  • 顺便说一句,有一个panel.rug,但它不会在绘图区域之外绘制地毯。

标签: r lattice


【解决方案1】:

您对panel.rug() 提出了一个好主意,但被lattice 将其绘图默认剪切到面板内部的方式阻碍了。为了解决这个问题,您可以通过 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)

【讨论】:

    最近更新 更多