Crayon Constantinople
Crayon Constantinople

Reputation: 265

Reduce tick marks in xyplot in R

I have tried looking for the answer to this but none of the solutions have worked. I have created a plot with the following code:

library(lattice)
xyplot(Births.Registered..Number. ~ X, data=CSOStats, scales=list(
y=list(tick.number = 10)))

I have been able to change the tick marks for the y axis. I want to decrease the amount of tick marks for the x axis as well. The dates on the x axis are in this format "1960Q1", "1960Q2", etc. I have not been able to reduce the amount of ticks on the graph while maintaining the names of the respective tick. Ideally, I would be able to see a tick every 5 years (which is every 20 ticks), but I can't achieve this. Here is a picture of the graph at present.

enter image description here

Upvotes: 1

Views: 6429

Answers (1)

sgibb
sgibb

Reputation: 25736

tick.number does not work for character or factor (from ?xyplot):

‘tick.number’ An integer, giving the suggested number of intervals between ticks. This is ignored for a factor, shingle, or character vector, for in these cases there is no natural rule for leaving out some of the labels.

But you could use at and labels, e.g.:

library("lattice")

## example data
df <- data.frame(x=paste0(rep(1960:1999, each=4), paste0("Q", 1:4)), y=1:160)

## calculate position and create labels
x.tick.number <- 10
at <- seq(1, nrow(df), length.out=x.tick.number)
labels <- round(seq(1960, 1999, length.out=x.tick.number))

xyplot(y ~ x, data=df, scales=list(y=list(tick.number=10), 
                                   x=list(at=at, labels=labels, rot=90)))

enter image description here

Upvotes: 1

Related Questions