Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading