2017-05-09 32 views
-1

我想生成一個顯示x軸日期的繪圖(Base R)。使用plot在x軸上設置日期

這是我到目前爲止的代碼:

plot(dr[[ncol(dr)]][-(length(dr[[ncol(dr)]]))], type = 'l', main = 
paste("Title - ", colnames(dr[ncol(dr)])), xlab = "", ylab = "", 
xaxt = 'n', col = "red", lwd = 2, cex.main = 2, ylim = c(0,0.5)) 
title(xlab = "Period", line=0.5, cex.lab = 1.5) 

如何把在x軸上的日期?

這裏的情節:

enter image description here

這是我的變量日期:

[1] "2008-11-28" "2008-12-31" "2009-01-30" "2009-02-27" "2009-03-31" "2009-04-30" "2009-05-29" "2009-06-30" "2009-07-31" 
[10] "2009-08-31" "2009-09-30" "2009-10-30" "2009-11-30" "2009-12-31" "2010-01-29" "2010-02-26" "2010-03-31" "2010-04-30" 
[19] "2010-05-31" "2010-06-30" "2010-07-30" "2010-08-31" "2010-09-30" "2010-10-29" "2010-11-30" "2010-12-31" "2011-01-31" 
[28] "2011-02-28" "2011-03-31" "2011-04-29" "2011-05-31" "2011-06-30" "2011-07-29" "2011-08-31" "2011-09-30" "2011-10-31" 
[37] "2011-11-30" "2011-12-30" "2012-01-31" "2012-02-29" "2012-03-30" "2012-04-30" "2012-05-31" "2012-06-29" "2012-07-31" 
[46] "2012-08-31" "2012-09-28" "2012-10-31" "2012-11-30" "2012-12-31" "2013-01-31" "2013-02-28" "2013-03-29" "2013-04-30" 
[55] "2013-05-31" "2013-06-28" "2013-07-31" "2013-08-30" "2013-09-30" "2013-10-31" "2013-11-29" "2013-12-31" "2014-01-31" 
[64] "2014-02-28" "2014-03-31" "2014-04-30" "2014-05-30" "2014-06-30" "2014-07-31" "2014-08-29" "2014-09-30" "2014-10-31" 
[73] "2014-11-28" "2014-12-31" "2015-01-30" "2015-02-27" "2015-03-31" "2015-04-30" "2015-05-29" "2015-06-30" "2015-07-31" 
[82] "2015-08-31" "2015-09-30" "2015-10-30" "2015-11-30" "2015-12-31" 
+1

請張貼重複的例子。除非我們知道您的X軸值是什麼結構,否則無法真正幫助您。 –

+0

我編輯了我的評論 – Luka

+0

請修復您的圖像鏈接。此外,審查[如何使一個偉大的可再生示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 –

回答

0

既然你錯過了你的變量y我會用您的變量日期放在一個例子。

data <- as.Date(data, format= "%Y-%m-%d", tz = "UTC", origin="1970-01-01") 
ats = seq(1, length(runif(length(data))),2) # this is the position 
labs = seq.Date(min(data), max(data), length.out = length(ats)) # these are the labels to plot in the x axis 
plot(runif(length(ats)) ~ ats , type = 'l', main = "Title ", xlab = "", ylab = "", 
    xaxt = 'n', col = "red", lwd = 2, cex.main = 2, ylim = c(0,1)) # note here that the values are randomly generated using `runif`. 
axis(1, at = ats, labels = labs, las= 2) 

輸出: enter image description here

數據:

data <- c("2008-11-28", "2008-12-31", "2009-01-30", "2009-02-27", "2009-03-31", "2009-04-30", "2009-05-29", "2009-06-30", "2009-07-31", 
"2009-08-31", "2009-09-30", "2009-10-30", "2009-11-30", "2009-12-31", "2010-01-29", "2010-02-26", "2010-03-31", "2010-04-30", 
"2010-05-31", "2010-06-30", "2010-07-30", "2010-08-31", "2010-09-30", "2010-10-29", "2010-11-30", "2010-12-31", "2011-01-31", 
"2011-02-28", "2011-03-31", "2011-04-29", "2011-05-31", "2011-06-30", "2011-07-29", "2011-08-31", "2011-09-30", "2011-10-31", 
"2011-11-30", "2011-12-30", "2012-01-31", "2012-02-29", "2012-03-30", "2012-04-30", "2012-05-31", "2012-06-29", "2012-07-31", 
"2012-08-31", "2012-09-28", "2012-10-31", "2012-11-30", "2012-12-31", "2013-01-31", "2013-02-28", "2013-03-29", "2013-04-30", 
"2013-05-31", "2013-06-28", "2013-07-31", "2013-08-30", "2013-09-30", "2013-10-31", "2013-11-29", "2013-12-31", "2014-01-31", 
"2014-02-28", "2014-03-31", "2014-04-30", "2014-05-30", "2014-06-30", "2014-07-31", "2014-08-29", "2014-09-30", "2014-10-31", 
"2014-11-28", "2014-12-31", "2015-01-30", "2015-02-27", "2015-03-31", "2015-04-30", "2015-05-29", "2015-06-30", "2015-07-31", 
"2015-08-31", "2015-09-30", "2015-10-30", "2015-11-30", "2015-12-31")