开发者

R x axis date label only one value

I'm creating a plot in R with dates as the xaxis. My frame has dates, no problem. I'm using custom date range - one that cuts off some of the earliest data by using a fixed start and extend slightly past the latest data by using a end determined by some other code. The range is ~47 days right now. That's all working fine.

My problem is that the xaxis label includes only a single label, "Feb" but I'd like to include at least 3 labels, if not 5.

starttime <- strptime("20110110", "%Y%m%d")
endtime <- strptime("20110226 1202", "%Y%m%d %H%M") #This is actually determined programmatically, but that's not important
xrange <- c(starttime, endtime)
yrange <- c(0, 100)
par(mar=par()$mar+c(0,0,0,7),bty="l")
plot(xrange, yrange, type="n", xlab="Submission Time开发者_Go百科", ylab="Best Score", main="Top Scores for each team over time")
#More code to loop and add a bunch of lines(), but it's not really relevant

The resulting graph looks like this:

R x axis date label only one value

I really just want better labels. I'm not too concerned about exactly what they are, but something with Month + Day, and at least 3 of them.


Try this. I changed your plot() statement a little and added two lines.

 starttime <- strptime("20110110", "%Y%m%d")
 endtime <- strptime("20110226 1202", "%Y%m%d %H%M")
 #This is actually determined programmatically, but that's not important
 xrange <- c(starttime, endtime)
 yrange <- c(0, 100)
 par(mar=par()$mar+c(0,0,0,7),bty="l")

 #I added xaxt="n" to supress the plotting of the x-axis
 plot(xrange, yrange, type="n", xaxt="n", xlab="Submission Time", ylab="Best Score", main="Top Scores for each team over time")

 #I added the following two lines to plot the x-axis with a label every 7 days
 atx <- seq(starttime, endtime, by=7*24*60*60)
 axis(1, at=atx, labels=format(atx, "%b\n%d"), padj=0.5)

 #More code to loop and add a bunch of lines(), but it's not really relevant

R x axis date label only one value


In addition, look at axis.Date(), which is not an S3 generic, but can help set up the extra labels you want. I wrote a patch for this function that got incorporated several R versions ago, which allowed axes without labels. Here is an example taken from ?axis.Date:

random.dates <- as.Date("2001/1/1") + 70*sort(stats::runif(100))
plot(random.dates, 1:100)
# or for a better axis labelling
plot(random.dates, 1:100, xaxt="n")
axis.Date(1, at=seq(as.Date("2001/1/1"), max(random.dates)+6, "weeks"))
axis.Date(1, at=seq(as.Date("2001/1/1"), max(random.dates)+6, "days"),
          labels = FALSE, tcl = -0.2)

which produces:

R x axis date label only one value

There is also Axis.Date() which is an S3 generic, so can be called via Axis(dates.vec, ....) where dates.vec is the x-axis vector of dates. at etc can also be specified.


You can do that by hand if you suppress the x-axis annotation in the call to plot() and then use axis() with manually specified points and labels:

axis(1,at=axis.pos[axis.ind],labels=axis.txt[axis.ind])

using a set of indices axis.ind which selects from x values and formatted labels. You can use strftime() for just about anything, eg '%d %b' should produce day and human-readable short months as in

R> strftime(Sys.Date(), "%d %b")
[1] "26 Feb"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜