Rafs
Rafs

Reputation: 812

Plotting gaps in time series as zeroes in R and ggplot2

ggplot2 is interpolating the missing measurements in the data. How to make it plot them as zeroes? Here is an example:

library(ggplot2)

x = c(1, 3, 5)  # The time axis
y = c(0.6, 0.4, 0.9)  # The measurements

ggplot(data.frame(x, y), aes(x, y)) +
    geom_line() +
    ylim(0, 1)

Output:

Interpolated line

Desired Output:

Desired line

Upvotes: 1

Views: 907

Answers (3)

Rafs
Rafs

Reputation: 812

Following @AllanCameron comments, the issue needs to be resolved on the data level and not on the visualisation level. For that reason, the records which hold zero measurements must be manually and explicitly appended to the data or else they will be interpolated.

There is no way to make ggplot's geom_line show a zero where a record does not exist in the data even with a discrete x-axis, and while this makes sense for certain applications, it is not generalisable.

Upvotes: 0

s_baldur
s_baldur

Reputation: 33488

Analogous to G. Grothendieck second solution but using data.table:

library(data.table)
setDT(df)
m <- df[list(x = seq(min(x), max(x))), on = "x"][, y := nafill(y, fill = 0)]

Data:

df <- data.frame(
  x = c(1, 3, 5), 
  y = c(0.6, 0.4, 0.9)
)

Upvotes: 0

G. Grothendieck
G. Grothendieck

Reputation: 269654

1) zoo Create a zoo object with frequency 1, convert that to ts class and back to zoo. Fill in the generated NAs with 0 and plot. (Add any desired geoms onto the end of the autoplot line using + as usual.)

library(ggplot2)
library(zoo)

z <- na.fill(as.zoo(as.ts(zoo(y, x, frequency = 1))), 0)
autoplot(z)

screenshot

2) merge Expand x and merge it with the original data. Then fill in the NAs with 0.

xx <- seq(min(x), max(x))
m <- merge(data.frame(x, y), data.frame(x = xx), all = TRUE)
m[is.na(m)] <- 0
ggplot(m, aes(x, y)) + geom_line()

Upvotes: 3

Related Questions