display ticks for every second observation (rotated labels)

元气小坏坏 提交于 2019-12-12 02:07:45

问题


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: http://i.imgur.com/CjIKFCV.jpg

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


回答1:


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()



来源:https://stackoverflow.com/questions/22516733/display-ticks-for-every-second-observation-rotated-labels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!