R – Exact axis ticks and labels in R Lattice xyplot

axis-labelslatticer

I'm creating many plots (each as a separate image) that all must have identical axis limits. When using ylim, the tick marks are awkwardly placed at the extreme edges and tick labels are omitted for the extreme values.

library(lattice)
x=1:100
y=10+20*(runif(100))
xyplot(y~x)                 # Case 1 - automatic ylim
xyplot(y~x, ylim=c(10,20))  # Case 2 - specified ylim

In Case 1, the axis ticks and labels are automatically generated at (y=10,15,20,25,30). All tick marks are labeled, and there is some vertical padding for the extreme tick marks (y=10 and y=30) in the plot rectangle.

In Case 2, when I specify ylim values, the tick marks are generated at (y=10,12,14,16,18,20) but labels appear only for (y=12,14,16,18). Tick labels are missing at the extremes. Also, there is no vertical padding for the extreme tick marks in the plot rectangle.

Is there a way to specify ylim and still have the tick marks and labels come out similarly to Case 1?

More generally, when specifying ylim:
1. how can you specify exactly where each tick mark goes?
2. how can you specify exactly which tick marks get labeled?

Best Answer

To get your tick marks and labels where you want, you can do something along the lines of:

xyplot(
  y~x,
  ylim=c(10,20),
  scales=list(
    y=list(
      at=seq(10,20,2),
      labels=c("a","","b","","c","") )
    )
)  

The padding issue, I'm not sure about how to address except for manually adjusting the ylim= arguments.

Related Topic