dtanon
dtanon

Reputation: 303

Time series plot in R: Remove gaps from plot [zoo]

I have created a zoo object by extracting a time series from a RasterBrick:

library(zoo)
s <- RasterBrick

target_cell <- 23265

ss <- zooExtract(s, target_cell)

windows()
plot.zoo(ss, type = "o")

My result is shown below. I want a line to go through all the points, but I can't seem to make it work, I have looked through previous answers for this questions. I have tried lines() and it gives same result.

Upvotes: 0

Views: 951

Answers (1)

Silence Dogood
Silence Dogood

Reputation: 3597

As @Richard Telford suggested the gaps in the plot are due to missing data. With base function complete.cases and na.locf from zoo the gaps could be removed or filled with previous observations as below:

#to identity periods with missing data

missingPeriod = as.Date(index(ss[!complete.cases(ss),]))


#to retain only periods with no missing data
ssComplete = ss[complete.cases(ss),]


#to retain all periods with gaps filled with previous value
#maxgap parameter controls number of missing data replaced with prev observation
N = 5
ssFilled = zoo::na.locf(ss,maxgap=N); 

#plots
plot.zoo(ssComplete , type = "o")

plot.zoo(ssFilled, type = "o") 

Upvotes: 1

Related Questions