Re: [R] geom_ribbon removes missing values

From: Karsten Loesing <karsten.loesing_at_gmx.net>
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[is.na(data$ymin) | is.na(data$ymax)] <- NA
+  for (i in 2:length(poly_ids))
+    if (!is.na(poly_ids[i]) & !is.na(poly_ids[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)),
         default.units="native",
         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)),
         default.units="native",
         gp=gpar(fill=NA, col=colour, lwd=size * .pt, lty=linetype)
       ))


Thanks,
--Karsten



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive