How can we alternate axis tick labels in lattice?

I wish to alternate Y-axis tick labels between left and right side. I checked the xyplot documentation, but couldn’t find any argument that would help with the goal. I tried using alternating argument to scales but it only accepts an integer or a vector with two integers. I wish to alternate tick labels since I want to visualise conditional modes estimated by a linear mixed effects model – with the default setting, sometimes, the Y-axis becomes too crowded.

Expected output:

z| --- |
 | --- |y
x| --- |
 | --- |w
 .  .
 .  .
 .  .
b| --- |
 | --- |a

Failed attempt:

library(lattice)
library(tidyverse)

set.seed(123)
latticeDF <- tibble(x = runif(26), y = letters)
lattice::dotplot(y ~ x, 
                 data = latticeDF, 
                 scales = list(y = list(labels = letters, 
                                       alternating = rep(c(1, 2), 13))))

Output:

enter image description here

>Solution :

The alternating argument determines on which side of each panel the whole axis is displayed. It does not support alternating labels to either side of the same panel. For this you would need a secondary axis, as produced by latticeExtra::doubleYScale.

In your case, that might look something like:

dp1 <- dotplot(y ~ x, data = latticeDF, 
               scales = list(y = list(labels = replace(letters, 1:13 * 2, ''))))

dp2 <- dotplot(y ~ x, data = latticeDF, 
               scales = list(y = list(labels = replace(letters, 1:13*2-1, ''))))

latticeExtra::doubleYScale(dp1, dp2, use.style = FALSE, add.ylab2 = TRUE)

enter image description here

Leave a Reply