[R] geom_ribbon removes missing values
Karsten Loesing
karsten.loesing at gmx.net
Thu Jun 10 09:30:19 CEST 2010
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
--- ggplot2-orig 2010-06-06 14:02:25.000000000 +0200
+++ ggplot2 2010-06-10 08:31:02.000000000 +0200
@@ -5044,9 +5044,16 @@
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
More information about the R-help
mailing list