Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting discontinuous xts timeseries on same plot in R?

Tags:

merge

plot

r

xts

I'd like to be able to do a line plot of several discontinuous time series on the same screen in R, and am having trouble getting all data to appear at once:

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)

Of course, when I plot as a point graph, everything shows up okay:

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

But when I try to plot one or more series as lines, the discontinuities cause trouble, e.g.:

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

How can I plot each of these series as a continuous line on the same set of axes?

like image 642
user1521655 Avatar asked Oct 05 '22 20:10

user1521655


1 Answers

xts and zoo behave consistent with the default plotting methods:

matplot(df1_xts_merge,type="l")

default plotting via matplot

You seem to want a plot of the lines interpolated through the points in your xts object, in which case na.approx or na.spline will be helpful:

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

xtsExtra::plot.xts with na.approx

like image 170
Joshua Ulrich Avatar answered Oct 10 '22 01:10

Joshua Ulrich