[R] geom_ribbon removes missing values
Karsten Loesing
karsten.loesing at gmx.net
Sun Jun 6 19:46:31 CEST 2010
Hi Hadley,
On 5/31/10 9:51 PM, Hadley Wickham wrote:
> There's no easy way to do this because behind the scenes geom_ribbon
> uses grid.polygon.
A possible workaround might be to have grid.polygon draw multiple
polygons, one for each interval. We can do this by constructing vectors
with coordinates for the first polygon, then NA, then coordinates for
the second polygon, etc. Here are the vectors for my initial example:
x <- c(x[1:4], x[4:1], NA, x[7:10], x[10:7])
y <- c(ymax[1:4], ymin[4:1], NA, ymax[7:10], ymin[10:7])
I worked on a simple (but ugly) patch to GeomRibbon in ggplot2 that does
the job using an iteration:
/Library/Frameworks/R.framework/Versions/2.10/Resources/library/ggplot2/R$
diff ggplot2-orig ggplot2
5047,5048d5046
< data <- remove_missing(data, na.rm,
< c("x","ymin","ymax"), name = "geom_ribbon")
5050a5049,5069
> start <- 0
> polyx <- c()
> polyy <- c()
> for (i in 1:(length(data$x)+1)) {
> if (i > length(data$x) || is.na(data$ymin[i]) ||
> is.na(data$ymax[i])) {
> if (start > 0) {
> polyx <- c(polyx, data$x[start:(i-1)],
> data$x[(i-1):start], NA)
> polyy <- c(polyy, data$ymax[start:(i-1)],
> data$ymin[start:(i-1)], NA)
> start <- 0
> }
> } else {
> if (start == 0) {
> start <- i
> }
> }
> }
> polyx <- head(polyx, length(polyx) - 1)
> polyy <- head(polyy, length(polyy) - 1)
5052c5071
< coordinates$munch(data.frame(x=c(x, rev(x)), y=c(ymax,
rev(ymin))), scales)
---
> coordinates$munch(data.frame(x = polyx, y = polyy), scales)
Do you like the described approach? Can you help me make my patch better?
In particular, I'd want to avoid iterating over the data frame and
extract start and end index of intervals separated by NA. Is there a
function for this or at least a better approach?
Also, probably a stupid question: How do I tell R to use the cloned
ggplot2 sources instead of the installed ggplot2 package? As you can
see, I modified the installed package, but I'd rather work with Git here.
Thanks,
--Karsten
> On Sun, May 30, 2010 at 7:26 AM, Karsten Loesing
> <karsten.loesing at gmx.net> wrote:
>> 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:
>>
>> library(ggplot2)
>> 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).
>>
>> 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.
>>
>
>
>
More information about the R-help
mailing list