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:
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)
If you are interested, you could do the same thing in ggplot like this:
library(ggplot2)
ggplot(latticeDF, aes(x, as.numeric(factor(y)))) +
geom_point(color = '#0080fe') +
scale_y_continuous('y', breaks = 1:13 * 2 - 1, minor_breaks = 1:13 * 2,
labels = ~letters[.x],
sec.axis = sec_axis(~.x, NULL, breaks = 1:13 * 2,
labels = letters[1:13 * 2])) +
theme_bw() +
theme(panel.grid = element_blank(),
panel.grid.major.y = element_line(linewidth = 0.2, color = 'gray'),
panel.grid.minor.y = element_line(linewidth = 0.2, color = 'gray'))