2011-02-26 71 views
3

我在R中創建了一個日期爲xaxis的圖。我的框架有日期,沒有問題。我正在使用自定義日期範圍 - 一種是通過使用固定的開始來切斷一些最早的數據,並通過使用由其他代碼確定的結束略微超過最新數據。現在範圍是〜47天。這一切工作正常。R x軸日期標籤只有一個值

我的問題是x軸標籤只包括一個標籤,「二月」,但我想至少包括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 

結果圖如下所示: enter image description here

我真的只想要更好的標籤。我並不太在意他們是什麼,但是有些月份+日,並且至少有3個。

+0

看看軸() – speendo 2011-02-26 17:14:13

+0

好吧,它似乎像axis(1)或axTicks(1)可能會自動做一些。我只需要弄清楚如何將它從想要使用的科學記數法改爲「Feb 26」之類的東西...... – chmullig 2011-02-26 17:42:09

+0

你也可以考慮看看'matplot(...,type ='s' )',而不是使用空的'plot()',然後單獨調用'lines()'。 – Charles 2011-02-27 19:27:51

回答

3

試試這個。我改變了你的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 

enter image description here

+1

謝謝,這太棒了。我可以使用'by =(endtime-starttime)/ 4'這樣的東西來生成5個均勻分佈的點,不管它有多大。這會在147天而不是47天時更好地擴展。 – chmullig 2011-02-27 00:04:21

+2

只需使用'len = N'參數來獲得'N'點---'seq()'已經可以做你在這裏的近似值了。 – 2011-02-27 00:07:09

+0

呵呵,謝謝@Dirk。 – chmullig 2011-02-27 00:39:15

2

你可以做到這一點的手,如果你抑制調用plot(),然後在x軸上標註使用axis()手工指定點和標籤:使用一組指標​​從選擇的

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

x值和格式標籤。您可以使用strftime()公正的東西,如'%d %b'應該生產日期和人類可讀的短短几個月中

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

這似乎是一條很好的路徑,但我不清楚我將如何使用這些值。我會選擇我的開始日期和結束日期,然後獲取中點日期嗎?所以它會像'axis(1,at = starttime,labels = strftime(starttime,「%d%b」))''這樣的命令。 – chmullig 2011-02-26 17:16:06

+0

你需要弄清楚。軸標籤*很難*。你在缺乏最佳結果的情況下捕獲了默認行爲,現在你需要忍受它,或者自己寫一點選擇,或者嘗試以不同的方式參數化默認值。也許設置明確的'xlim'限制將會有所幫助。我們無法確定您是否提供了(模擬)數據以使其具有可重複性。 – 2011-02-26 17:44:10

+0

我知道這很難 - 這就是爲什麼我要求幫助!如果這很容易,我會想出來。我提供了確切的代碼來生成軸,就像我的例子中看到的一樣。線條與問題無關。如果你運行我在這個問題中給出的代碼,你將得到沒有行的同一個圖,完全適合這個目的。 – chmullig 2011-02-26 23:58:39

3

另外,看axis.Date(),這是的S3通用的,但可以幫助建立所需的額外的標籤。我爲這個功能編寫了一個補丁,它在幾個R版本之前包含了它,允許沒有標籤的軸。這裏是從?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) 

其產生: enter image description here

還有Axis.Date(),其一個S3通用的,所以可以通過Axis(dates.vec, ....)其中dates.vec被稱爲是x軸矢量的日期。 at等也可以指定。