user3439329
user3439329

Reputation: 891

display ticks for every second observation (rotated labels)

Having trouble getting my x-axis to look right.

I would like the ticks and labels to appear for every second observation. Unfortunately, while my code displays ticks appropriately, label values increase by 1 each time.

In other words I would like the tick labels to be 2011-10-21, 2011-10-23, 2011-10-25.... My code currently produces tick labels as2011-10-21, 2011-10-22, 2011-10-23 ...

My data frame looks like:

date comp_count
1 2011-10-21         10
2 2011-10-22          3
3 2011-10-23          1
4 2011-10-24          1
5 2011-10-25          1
6 2011-10-26          2

Here is the code I'm running:

plot(my_data$comp_count, main="comped sites over time", col="blue", type='l', lwd=2,       axes=FALSE, xlab="date", ylab="count")

axis(1,at=2*0:nrow(my_data), lab=FALSE)

text(2*0:nrow(my_data), par("usr")[3]-1, lab=my_data$date, srt=45, adj=1, xpd=TRUE, cex=0.8)

My chart looks like: https://i.sstatic.net/pqChL.png

I would like to keep the tick labels rotated since I will be using much more data in the end.

Upvotes: 0

Views: 836

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99361

Is this what you're looking for?

> with(my_data, plot(comp_count, axes = FALSE, xlab = "", type = 'l'))
> axis(2)
> axis(1, at = my_data$date, labels = FALSE)
> ticks <- my_data$date[c(TRUE, FALSE)]
> text(seq(1, length(my_data$date), 2), par("usr")[3] - 0.25, srt = 45, 
       adj = 1, labels = ticks, xpd = TRUE)
> box()

enter image description here

Upvotes: 1

Related Questions