Re: [R] geom_ribbon removes missing values

From: Karsten Loesing <>
Date: Thu, 10 Jun 2010 01:37:57 +0200

Hi Paul,

On 6/9/10 1:12 AM, Paul Murrell wrote:
> grid.polygon() can do multiple polygons in a single call, but rather
> than using NA's to separate sub-polygons, it uses an 'id' argument (or
> an 'id.lengths' argument) to identify sub-polygons within the vectors of
> x- and y-values (see the examples in ?grid.polygon). So a ggplot2 patch
> that makes use of that facility might make more sense.

That's a great idea! And it makes the patch look far less ugly. Thanks for that!

I still can't get rid of the loop, but I'd guess that going through the vector once is not a performance killer. If someone has an idea how we can get a similar vector as the one mentioned in the comment, but without using a loop, please do tell!

Here's the new patch:

   draw <- function(., data, scales, coordinates, na.rm = FALSE, ...) { - data <- remove_missing(data, na.rm,

-      c("x","ymin","ymax"), name = "geom_ribbon")
     data <- data[order(data$group, data$x), ]
+  # Instead of removing NA values from the data and plotting a single
+  # polygon, we want to "stop" plotting the polygon whenever we're missing
+  # values and "start" a new polygon as soon as we have new values.  We do
+  # this by creating an id vector for polygonGrob that has distinct
+  # polygon numbers for sequences of non-NA values and NA for NA values in
+  # the original data.  Example: c(NA, 2, 2, 2, NA, NA, 7, 7, 7, NA)
+  poly_ids <- 1:length(data$x)
+  poly_ids[$ymin) |$ymax)] <- NA
+  for (i in 2:length(poly_ids))
+    if (![i]) & ![i-1]))
+      poly_ids[i] <- poly_ids[i-1]

     tb <- with(data,
       coordinates$munch(data.frame(x=c(x, rev(x)), y=c(ymax,
rev(ymin))), scales)
@@ -5054,12 +5064,12 @@
     with(data, ggname(.$my_name(), gTree(children=gList(
       ggname("fill", polygonGrob(
-        tb$x, tb$y,
+        tb$x, tb$y, id=c(poly_ids, rev(poly_ids)),
         gp=gpar(fill=alpha(fill, alpha), col=NA)
       ggname("outline", polygonGrob(
-        tb$x, tb$y,
+        tb$x, tb$y, id=c(poly_ids, rev(poly_ids)),
         gp=gpar(fill=NA, col=colour, lwd=size * .pt, lty=linetype)

--Karsten mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 Jun 2010 - 23:40:13 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Jun 2010 - 00:50:30 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive