2012-11-28 69 views
3

我希望能夠做的幾個不連續的時間序列線圖中的R相同的屏幕上,和我有麻煩的所有數據出現一次:在R中的同一圖上繪製不連續的xts時間序列?

require(xts) 
require(xtsExtra) 
df1=data.frame(a=1:30,b=3*1:30) 
df1$b[2*1:15]=NA 
df1A_xts=xts(df1,ISOdate(1900+1:30,1,1)) 
df1B_xts=xts(df1,ISOdate(1900+2*1:30,2,1)) 
df1_xts_merge=merge.xts(df1A_xts,df1B_xts) 

當然,當我情節作爲一個點圖,一切都還好吧顯示出來:

plot.xts(df1_xts_merge,screens=1,type="p",auto.legend=TRUE) 

但是當我嘗試繪製一個或多個系列的線條,不連續性造成麻煩,比如:

plot.xts(df1_xts_merge,screens=1,auto.legend=TRUE) 

如何將這些系列中的每一個繪製爲同一組軸上的連續線?

+0

不知道,但看起來它與'NA'有關。這工作:'plot(na.omit(df1A_xts [,2]))',但這不'plot(df1A_xts [,2])' – GSee

+0

我同意這些問題。我會開放的解決方案,以某種方式將na.omit()數據集合並在一個圖上。我還沒有想出如何讓這個工作。 – user1521655

+0

好吧,這是一個有點不令人滿意的解決方案,但它的工作原理如下: 'plot(index(na.omit(df1A_xts)),na.omit(df1A_xts)$ a,type =「l」)' 'lines(index( na.omit(df1A_xts)),na.omit(df1A_xts)$ b,type =「l」)' 'lines(index(na.omit(df1B_xts)),na.omit(df1B_xts)$ b,type =「 (索引(na.omit(df1B_xts)),na.omit(df1B_xts)$ a,type =「l」)' – user1521655

回答

3

XTS和動物園的行爲與默認的繪圖方法一致:

matplot(df1_xts_merge,type="l") 

default plotting via matplot

你似乎想通過這些點在你的XTS插對象線條的曲線圖,在這種情況下na.approxna.spline會有所幫助:

plot(na.approx(df1_xts_merge),screens=1,auto.legend=TRUE) 

xtsExtra::plot.xts with na.approx