使用 `c.trellis` 时维护自定义刻度标签

Maintain custom tick labels when using `c.trellis`

我有两个网格对象,我想使用 latticeExtra 中的 c.trellis 组合它们(这两个图可以下载 here)。正如您在下面看到的,生成的图继承了第一个图中的刻度标签,而第二个图中的标签被丢弃了。使用 c.trellis 时是否可以保留不同的 y 轴刻度标签?

library(latticeExtra)

rsq_plt <- readRDS("rsq.rds")
err_plt <- readRDS("err.rds")

latticeExtra:::c.trellis(rsq_plt, err_plt, layout = c(1, 2))

仅作记录,由于网格图的全面自定义选项,我似乎终于想出了一个合适的解决方案。在执行 c.trellis 之前禁用上图中的 scales(通过 scales = list(draw = FALSE);注意文件 'rsq.rds' 已在线更改),随后 update-ing 组合使用自定义 y 轴绘图解决了这个问题。

## combine plots and increase left padding
plt <- latticeExtra:::c.trellis(rsq_plt, err_plt, layout = c(1, 2))
plt <- update(plt, 
              scales = list(draw = FALSE), 
              par.settings = list(
                layout.widths = list(left.padding = 6, right.padding = 0), 
                layout.heights = list(top.padding = 0, bottom.padding = 0)
              ))

## custom panel.axis
panel.fun <- function(...) {

  # allow to draw labels outside panel
  trellis.par.set("clip", list(panel = "off", strip = "off"))

  # add upper y-axis
  if (panel.number() == 1) {
    panel.axis("left", at = 1, tck = .5, outside = TRUE,
               labels = expression("r"^2))
    panel.abline(v = 1, lty = 3, lwd = 1, col = "red")
    panel.dotplot(lwd = .5, ...)
  }

  # add lower y-axis
  if (panel.number() == 2) {
    panel.axis("left", at = 2:4, outside = TRUE, tck = .5,
               labels = c("MAE", "ME", "RMSE"))
    panel.abline(v = 0, lty = 3, lwd = 1, col = "red")
    panel.dotplot(..., lwd = 0.5)
  }
}

## apply custom axes
update(plt, panel = panel.fun)