title: Stacked cumulative histograms/area plot with R
body: <p>I have some data which looks like</p>
  
  <pre><code>library(data.table)
  set.seed(1)
  data &lt;- data.table( sample(1:6,1000,replace=T), rnorm(1000, mean = 5))
       V1       V2
     1:  2 5.077303
     2:  3 4.703131
     3:  4 3.816758
     4:  6 5.011293
     5:  2 5.991601
    ---            
   996:  5 5.114406
   997:  4 4.607735
   998:  2 6.652358
   999:  2 3.756302
  1000:  2 4.088074
  </code></pre>
  
  <p>I want to create a plot with some sort of cumulative histograms or cumulative area graph filled by V1 and with V2 on the x-axis, stacked on top of each other. The result should look something like this:</p>
  
  <p><a href="https://i.imgur.com/GGywhzu.png" rel="nofollow noreferrer">http://i.imgur.com/GGywhzu.png</a></p>
  
  <p>This is what I tried so far:</p>
  
  <pre><code>library(ggplot2)
  ggplot(data, aes(group=V1, x = V2, fill=as.factor(V1))) +
      geom_histogram(aes(y=cumsum(..count..)/sum(..counts..)), bins = 50)
  </code></pre>
  
  <p>However it does not stack the data correctly onto each other. It would also be nicer with an area plot, instead of the histogram bars. This is what the above code produces:
  <a href="https://i.stack.imgur.com/TYswP.png" rel="nofollow noreferrer">plot</a></p>