[R] shading in overlap between two ranges

Graves, Gregory ggraves at sfwmd.gov
Mon Jun 6 17:03:02 CEST 2011


This worked perfectly.  An example graphic is located here:
ftp://ftp.sfwmd.gov/pub/ggraves/ribbon.bmp


-----Original Message-----
From: Dennis Murphy [mailto:djmuser at gmail.com] 
Sent: Thursday, June 02, 2011 12:11 PM
To: Graves, Gregory
Cc: r-help at r-project.org; Kemp, Susan K SAJ; Patrick_Pitts at fws.gov
Subject: Re: [R] shading in overlap between two ranges

Hi:

Here's one approach using geom_ribbon() in ggplot2 - the 'overlap' is
the change in color where the two ribbons intersect. Using your
example data with the same names and the 'one.month' variable removed,

library(ggplot2)
ggplot() +
  geom_ribbon(data = target, aes(x = i.value, ymin = X25, ymax = X75,
                 fill = 'Target'), alpha = 0.4) +
  geom_ribbon(data = observed, aes(x = i.value, ymin = X25, ymax = X75,
                 fill = 'Observed'), alpha = 0.4) +
  scale_fill_manual("", c('Target' = 'blue', 'Observed' = 'orange')) +
  opts(legend.position = c(0.88, 0.85),
       legend.background = theme_rect(colour = 'transparent'),
       legend.text = theme_text(size = 12)) +
  labs(x = 'Month', y = 'Value')

There is a separate geom_ribbon() for each of target and observed. A
factor variable for fill color is generated on the fly with colors
specified in scale_fill_manual(). The opts() reposition the legend
inside the graphics region (the values represent proportions of the
total graphics area in each direction), make the legend background
transparent and slightly increase the size of the legend labels
(default size = 10 in theme_text).
Alpha transparency is used so that the overlap creates a blend of the
two colors; without it, one overwrites the other.

HTH,
Dennis


On Thu, Jun 2, 2011 at 8:04 AM, Graves, Gregory <ggraves at sfwmd.gov> wrote:
> I have 2 datafiles 'target' and 'observed' as shown below (I will gladly
> email these 2 small files to whomever).  X25. And X75. Indicate the
> value of 25th and 75th-percentile of the target ('what should be') and
> the observed ('what is').  The i.value is simply the month.
>
>> target
>        X        i.value    X25.     X75.
> 1  one.month       1 10.845225 17.87237
> 2  one.month       2 12.235813 19.74490
> 3  one.month       3 14.611749 23.44810
> 4  one.month       4 17.529332 28.09647
> 5  one.month       5 19.458738 30.56936
> 6  one.month       6 15.264505 28.29333
> 7  one.month       7 12.370369 23.35455
> 8  one.month       8 12.471224 21.82794
> 9  one.month       9  9.716685 17.28762
> 10 one.month      10  6.470568 12.49830
> 11 one.month      11  6.180560 14.24961
> 12 one.month      12  9.673738 15.79208
>
>> observed
>     X         i.value   X25.     X75.
> 1  one.month       1 19.81000 27.63500
> 2  one.month       2 23.64062 30.09125
> 3  one.month       3 26.04865 35.99104
> 4  one.month       4 32.02625 41.50958
> 5  one.month       5 34.74479 47.75958
> 6  one.month       6 37.48885 46.56448
> 7  one.month       7 30.06740 40.10146
> 8  one.month       8 26.14917 39.49458
> 9  one.month       9 14.12521 32.39406
> 10 one.month      10 11.04125 23.55479
> 11 one.month      11 13.14917 23.56833
> 12 one.month      12 17.17938 27.02458
>
> The following plots 4 lines on one graph.  The area between the two red
> lines represents the target 'zone', and the area between the two black
> lines is the observed 'zone'.
>
> with(target, plot(X25.~i.value,ylim=c(0,55),type='l',col='red'))
> par(new=T)
> with(target, plot(X75.~i.value,ylim=c(0,55),type='l',col='red'))
> par(new=T)
> with(observed, plot(X25.~i.value,ylim=c(0,55),type='l'))
> par(new=T)
> with(observed, plot(X75.~i.value,ylim=c(0,55),type='l'))
> par(new=F)
>
> Ideally, the target and the observed should overlap in every month -
> they don't.  The desire is to visually accentuate the amount of overlap
> by shading in the area where these two "zones" overlap.  How would you
> do that?  Note, that in some of these characterizations, the overlap
> wanders in and out [I already have routines that calculate the percent
> of overlap, but I have been requested to find a way to shade the
> overlap.]
>
> ______________________________________________
> 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