DeltaIV
DeltaIV

Reputation: 5646

how to fill the gap between two curves in a ggplot plot

I want to fit a simple third degree polynomial to data, and then build a plot with three different geometries: the set of data points on which I fit the model (geom_point, in blue), the fit to these points + prediction intervals (geom_line, also in blue) and a prediction for days following to those to which the model was fit (geom_line, in red). This is my code:

library(ggplot2)

positives <- c(13, 65, 118, 229, 322, 455, 655, 888, 1128, 1577)
days_passed <- length(positives)
t <- seq(1, days_passed)
t_full <- c(t, seq(t[days_passed], 30))

model <- lm(positives ~ poly(t,degree=3))

predict_positives <- predict(model, list(t = t_full), interval = "prediction")

# plot
length(positives) <- length(t_full)
dframe <- data.frame(day = t_full, 
                     positives = positives,
                     future = ifelse(t_full <= days_passed, "N", "Y"),
                     lwr = predict_positives[, "lwr"],
                     fit = predict_positives[, "fit"],
                     upr = predict_positives[, "upr"])
p <- ggplot(dframe, aes(x = day, y = positives, color = future)) +
  geom_point() +
  geom_line(aes(y = fit)) +
  geom_line(aes(y = lwr), linetype = "dashed") +
  geom_line(aes(y = upr), linetype = "dashed") +
  geom_vline(xintercept = 17, linetype = "dashed") + 
  geom_vline(xintercept = 24)

print(p)

However, the resulting plot shows an unaesthetic gap:

enter image description here

how can I modify my code so that the fit and the prediction are contiguous?

Upvotes: 3

Views: 304

Answers (1)

caldwellst
caldwellst

Reputation: 5956

Your future column has values of N for both rows corresponding to day 10. If you just change that, the plot looks how you intended.

dframe$future[11] <- "Y"

ggplot(dframe, aes(x = day, y = positives, color = future)) +
  geom_point() +
  geom_line(aes(y = fit)) +
  geom_line(aes(y = lwr), linetype = "dashed") +
  geom_line(aes(y = upr), linetype = "dashed") +
  geom_vline(xintercept = 17, linetype = "dashed") + 
  geom_vline(xintercept = 24)

enter image description here

Upvotes: 2

Related Questions