1

When using latticeExtra:::c.trellis to combine plots, the right-side tick marks and text/numeric labels go missing, and I'd like to bring them back:

library(latticeExtra)

set.seed(1)
foo <- data.frame(x = 1:100, 
                  y = 1:100 + rnorm(100))
foo$resid <- with(foo, x-y)

## Plot 1 -----
(p1 <- xyplot(y~x, foo))

## Plot 2 -----
(p2 <- 
    xyplot(resid~x, foo, 
           scales = list(rot = 0, tck = c(1,1), alternating = 3),
           between = list(y = 1), ylab.right = "ylab.right", 
           # par.settings = list(axis.components = 
           #                       list(right = list(pad1 = 2, pad2 = 2)))
           # Note: this padding attempt does not restore the missing ticks,
           # pad arguments get ignored when using c.trellis below
           ))
# tick marks appear on all four sides (as desired)

## Combine -----
(p12 <- latticeExtra:::c.trellis(p2, p1,layout = c(1,2)))
# right tick marks are missing

Is there a way to restore the right-side ticks and/or labels manually, say, by modifying the combined trellis object?

Bryan
  • 933
  • 1
  • 7
  • 21

1 Answers1

0

From the help file ?c.trellis:

Description

Combine the panels of multiple trellis objects into one.

and later,

Note that combining panels from different types of plots does not really fit the trellis model. Some features of the plot may not work as expected. In particular, some work may be needed to show or hide scales on selected panels. An example is given below.

It looks to me that you really aren't trying to combine panels into one object. You even use between to put some separation. Rather, you are trying to combine two plots.

You can use print,

print(p1,split=c(1,1,1,2),more=TRUE)
print(p2,split=c(1,2,1,2),more=FALSE)

See ?print.trellis.

DaveTurek
  • 1,297
  • 7
  • 8
  • Almost! The issue now becomes one of alignment. When running the `print` version, the x-axes do not perfectly align since space for y-axis labels is allocated automatically and differs by plot: p1 limits go from 0 to 100, which requires more horizontal space than the -2 to +2 on p2. As a result, the x-axis tick marks mis-align. They do align when using `c.trellis`. – Bryan Aug 26 '19 at 18:12
  • Fair enough. I guess still "some work may be needed to show or hide scales on selected panels." Or if you don't otherwise need `lattice`, I've constructed combined plots like you want in a single base plot. – DaveTurek Sep 03 '19 at 12:42
  • Thinking a bit more: `lattice` helpfully adjusts the margins to fit best, in base graphics the margins are fixed, either by default or using `par(mar=c(...))`. So two base plots using `par(mfrow=c(2,1))` would align. (Rather than constructing a combined plot as I referred to yesterday.) – DaveTurek Sep 04 '19 at 14:59