简体   繁体   中英

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.

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") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM