0

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

library(latticeExtra)

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

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

阴谋

4

1 回答 1

0

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

## 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)

解决方案

于 2016-02-18T15:16:31.700 回答