简体   繁体   English

R x轴日期标签只有一个值

[英]R x axis date label only one value

I'm creating a plot in R with dates as the xaxis. 我在R中创建了一个以日期为x轴的情节。 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. 现在的范围是~47天。 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. 我的问题是xaxis标签只包含一个标签“Feb”,但我想包含至少3个标签,如果不是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", 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: 结果图如下所示: 在此输入图像描述

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. 我并不太关心它们究竟是什么,而是关注月份+日,以及其中至少3个。

Try this. 尝试这个。 I changed your plot() statement a little and added two lines. 我稍微更改了plot()语句并添加了两行。

 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

在此输入图像描述

In addition, look at axis.Date() , which is not an S3 generic, but can help set up the extra labels you want. 另外,请查看axis.Date() ,它不是 S3泛型,但可以帮助设置所需的额外标签。 I wrote a patch for this function that got incorporated several R versions ago, which allowed axes without labels. 我为这个函数编写了一个补丁,它在几个R版本之前就已合并,它允许没有标签的轴。 Here is an example taken from ?axis.Date : 这是一个取自?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: 产生: 在此输入图像描述

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. Axis.Date() 也是 S3泛型,因此可以通过Axis(dates.vec, ....)其中dates.vec是日期的x轴向量。 at etc can also be specified. at etc也可以指定。

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: 如果在plot()调用中禁止x轴注释,然后使用带有手动指定点和标签的axis() ,则可以手动执行此操作:

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. 使用一组索引axis.ind ,它从x值和格式化标签中选择。 You can use strftime() for just about anything, eg '%d %b' should produce day and human-readable short months as in 你可以使用strftime()几乎任何东西,例如'%d %b'应该产生白天和人类可读的短月,如同

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM