rlatticetrellis

Maintain custom tick labels when using `c.trellis`


I have two trellis objects which I would like to combine using c.trellis from latticeExtra (the two figures can be downloaded here). As you can see below, the resulting plot inherits the tick labels from the first figure, whereas the labels from the second figure are discarded. Is it possible to keep different y-axis tick labels when using 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))

plot


Solution

  • Just for the record, it seems like I finally came up with a proper solution thanks to the comprehensive customization options for trellis plots. Disabling scales in the upper plot (via scales = list(draw = FALSE); note that the file 'rsq.rds' has changed online) prior to performing c.trellis and subsequently update-ing the combined plot with customized y-axes solved the issue.

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

    solution