title: Shading between two curves using geom_polygon
body: <p>I have tried to reproduce the plot from the package survIDINRI using ggplot. Below it is an example presented in the documentation. </p>
  
  <pre><code>#--- sample data (pbc in survival package) ---
  D=subset(pbc, select=c("time","status","age","albumin","edema","protime","bili"))
  D$status=as.numeric(D$status==2)
  D=D[!is.na(apply(D,1,mean)),]
  mydata=D[1:100,]
  
  t0=365*5
  indata1=mydata; 
  indata0=mydata[,-7]
  covs1&lt;-as.matrix(indata1[,c(-1,-2)])
  covs0&lt;-as.matrix(indata0[,c(-1,-2)])
  
  #--- inference ---
  x&lt;-IDI.INF(mydata[,1:2], covs0, covs1, t0, npert=200) 
  
  #--- Graphical presentation of the estimates ---
  IDI.INF.GRAPH(x)
  </code></pre>
  
  <p>Editing the function IDI.INF.GRAPH, I wrote the code below: </p>
  
  <pre><code>library(ggplot2)
  library(tidyr)
  s &lt;- x$point$cc
  event &lt;- 1 - x$point$FX
  nevent &lt;- 1 - x$point$GX
  Xlim &lt;- c(min(s[event &gt; 0.001 | nevent &gt; 0.001]), 
            max(s[event &lt; 0.999 | nevent &lt; 0.999]))
  Ylim &lt;- c(0, 1)
  class &lt;- ifelse(nevent &gt; event, "blue", "red")
  
  data_plot &lt;- data.frame(event, nevent, s, class, min = event, max = nevent)
  data_plot &lt;- data_plot %&gt;% gather(key = curve, value = value, event:nevent)
  
  gp &lt;- ggplot(data_plot, aes(x = s, y = value)) +
    geom_line(aes(linetype = curve), size = 1.2)  +
    geom_ribbon(aes(x = s, ymin = min, ymax = max, fill = class)) +
    scale_linetype("C.D.F.", labels = c("Event", "Non-Event")) +
    labs(y = "P(D &lt; s)", x = "s") +
    theme(legend.position = "bottom") +
    theme_bw() 
  </code></pre>
  
  <p>It seems geom_ribbon is not adequate since I have two non-continuous areas. Then I tried geom_polygon:</p>
  
  <pre><code>s &lt;- x$point$cc
  event &lt;- 1 - x$point$FX
  nevent &lt;- 1 - x$point$GX
  Xlim &lt;- c(min(s[event &gt; 0.001 | nevent &gt; 0.001]), 
            max(s[event &lt; 0.999 | nevent &lt; 0.999]))
  Ylim &lt;- c(0, 1)
  class &lt;- ifelse(nevent &gt; event, "blue", "red")
  
  data_plot &lt;- data.frame(event, nevent, s, class)
  data_plot &lt;- data_plot %&gt;% gather(key = curve, value = value, event:nevent)
  
  ggplot(data_plot, aes(x = s, y = value)) +
      geom_line(aes(linetype = curve), size = 1.2)  +
      geom_polygon(data = subset(data_plot, class == "blue"), 
                  aes(x = s, y = value, group = class), fill = "blue") +
      geom_polygon(data = subset(data_plot, class == "red"), 
                  aes(x = s, y = value), fill = "red") +
      scale_linetype("C.D.F.", labels = c("Event", "Non-Event")) +
      labs(y = "P(D &lt; s)", x = "s") +
      theme(legend.position = "bottom") +
      theme_bw()
  </code></pre>
  
  <p>I know that I am not using geom_polygon properly, although I could not find a solution yet. Any ideas?</p>