Re: [R] geom_ribbon removes missing values

From: Karsten Loesing <karsten.loesing_at_gmx.net>
Date: Thu, 10 Jun 2010 09:30:19 +0200

Hi William,

On 6/10/10 2:07 AM, William Dunlap wrote:
> I'm not sure exactly what you want in poly_ids, but
> if x is a vector of numbers that might contain NA's
> and you want a vector of integers that identify each
> run of non-NA's and are NA for each then you can get
> it with
> poly_id <- cumsum(is.na(x)) + 1 # bump count for each NA seen
> poly_id[is.na(x)] <- NA
> E.g.,
> > x<-c(1.5, 2.5, NA, 4.5, 5.5, 6.5, NA, 8.5, 9.5, NA, NA, 12.5)
> > poly_ids <- cumsum(is.na(x)) + 1
> > poly_ids[is.na(x)] <- NA
> > rbind(x, poly_ids) # to line up input and output
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
> [,12]
> x 1.5 2.5 NA 4.5 5.5 6.5 NA 8.5 9.5 NA NA
> 12.5
> poly_ids 1.0 1.0 NA 2.0 2.0 2.0 NA 3.0 3.0 NA NA
> 5.0

Great! That's exactly what I want in poly_ids. Thanks! Please find the new patch below.

I also put a new branch on GitHub that is based on ggplot2 master and that has this patch. Note that I still don't know how to run ggplot2 from sources, so you'll have to trust in my copy-and-paste fu:

  http://github.com/kloesing/ggplot2/commit/177e69ae654da074

   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, 4, 4, 4, NA)
+ poly_ids <- cumsum(is.na(data$ymin) | is.na(data$ymax)) +1 + poly_ids[is.na(data$ymin) | is.na(data$ymax)] <- NA
     tb <- with(data,
       coordinates$munch(data.frame(x=c(x, rev(x)), y=c(ymax,
rev(ymin))), scales)
@@ -5054,12 +5061,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)
       ))

Best,
--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 Thu 10 Jun 2010 - 07:32:58 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 - 08:00:29 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