[R] geom_ribbon removes missing values

Karsten Loesing karsten.loesing at gmx.net
Sun May 30 14:26:43 CEST 2010

Hi everyone,

it looks like geom_ribbon removes missing values and plots a single
ribbon over the whole interval of x values. However, I'd rather want it
to act like geom_line, that is, interrupt the ribbon for the interval of
missing values and continue once there are new values. Here's an example:

df <- data.frame(
  date = seq(from = as.Date("2010-05-15"),
             to = as.Date("2010-05-24"),
             by = "1 day"),
  low = c(4, 5, 4, 5, NA, NA, 4, 5, 4, 5),
  mid = c(8, 9, 8, 9, NA, NA, 8, 9, 8, 9),
  high = c(12, 13, 12, 13, NA, NA, 12, 13, 12, 13))
ggplot(df, aes(x = date, y = mid, ymin = low, ymax = high)) +
  geom_line() +
  geom_ribbon(fill = alpha("blue", 0.5))

When running this code, R tells me:

Warning message:
Removed 2 rows containing missing values (geom_ribbon).

When you look at the graph, you can see that the line stops at May 18
and starts again on May 21. But the ribbon reaches from May 15 to 24,
even though there are no values on May 19 and 20.

Is there an option that I could set? Or a geom/stat that I should use
instead? In my pre-ggplot2 times I used polygon(), but I figured there
must be something better in ggplot2 (as there has always been so far).


More information about the R-help mailing list