我有一個數據框有兩個變量。其中一個是日期(Col1),另一個是數字人口數據(sk)。我可以繪製這樣的數據:如何在x軸中包含日期?
plot(D$sk)
我的問題是(顯然)該圖不包括X軸中的日期。我曾嘗試通過這個代碼,將其添加:
plot(D$sk, D$Col1, ylim=c(2013-01,2015-05))
但是我收到一個空白的情節和以下錯誤消息:
Warning message:
In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
基於this回答類似的問題,我使用了嘗試xaxt=n
這樣的說法:
with(D, plot(Col1, sk, xaxt="n"))
但它也不起作用。我得到了以下錯誤:
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
你可以請幫助我如何前進嗎?
EDIT:Col1
被該代碼定義:
Col1=seq(as.Date("2013/1/1"), by="1 month", length.out=41).
的sk
變量是100.000和200.000之間的一些隨機數。我很抱歉,但我是R新手,不確定如何重新評估這一點。我已經從41個excel表格中提取了它,並且轉換了幾次。但基本上這些是數據框架中的兩個變量。我希望這有些幫助。
EDIT2:對不起,剛纔注意到還有另外一個代碼,減少了日期變量:
D$Col1=format(D$Col1, format="%Y-%m")
你可以給你的數據的一個例子嗎? 或先看到這篇文章:http://stackoverflow.com/questions/4843969/plotting-time-series-with-date-labels-on-x-axis –
確保日期列是模式日期。如果不嘗試'as.Date(D $ Col1,format ='%Y-%m')'。格式可能不同 –
'2013-01'將評估爲2012!需要用引號括起來,可能需要用'as.Date'作爲日期對象 –