title: How to smooth ecdf plots in r
body: <p>I have a <code>df</code> with <code>5</code> variables, </p>
  
  <p>head(df,15)</p>
  
  <pre><code>               junc  N1.ir  N2.ir    W1.ir    W2.ir    W3.ir
  1  pos$chr1:3197398  0.000000  0.000000  0.000000  0.000000  0.000000
  2  pos$chr1:3207049  0.000000  0.000000  0.000000  0.000000  0.000000
  3  pos$chr1:3411982  0.000000  0.000000  0.000000  0.000000  0.000000
  4  pos$chr1:4342162  0.000000  0.000000  0.000000  0.000000  0.000000
  5  pos$chr1:4342918  0.000000  0.000000  0.000000  0.000000  0.000000
  6  pos$chr1:4767729 -4.369234 -5.123382 -4.738768 -4.643856 -5.034646
  7  pos$chr1:4772814 -3.841302 -3.891419 -4.025029 -3.643856 -3.184425
  8  pos$chr1:4798063 -5.038919 -4.847997 -5.497187 -4.035624 -7.543032
  9  pos$chr1:4798567 -4.735325 -5.096862 -3.882643 -3.227069 -4.983808
  10 pos$chr1:4818730 -8.366322 -7.118941 -8.280771 -6.629357 -6.876517
  11 pos$chr1:4820396 -5.514573 -6.330917 -5.898853 -4.700440 -5.830075
  12 pos$chr1:4822462 -5.580662 -6.914883 -5.562242 -5.380822 -5.703211
  13 pos$chr1:4827155 -4.333273 -4.600904 -4.133399 -4.012824 -3.708345
  14 pos$chr1:4829569 -4.287866 -3.874469 -3.977280 -4.209453 -4.490326
  15 pos$chr1:4857613 -6.902074 -6.074141 -6.116864 -3.989946 -6.474259
  </code></pre>
  
  <p>Few lines after using <code>melt</code></p>
  
  <pre><code>&gt; head(ir.m)
                junc variable     value
  1 pos$chr1:3197398 N1.ir  0.000000
  2 pos$chr1:3207049 N1.ir  0.000000
  3 pos$chr1:3411982 N1.ir  0.000000
  4 pos$chr1:4342162 N1.ir  0.000000
  5 pos$chr1:4342918 N1.ir  0.000000
  6 pos$chr1:4767729 N1.ir -4.369234
  </code></pre>
  
  <p>And summary </p>
  
  <pre><code>&gt; summary(ir)
                   junc           N1.ir          N2.ir           W1.ir       
   neg$chr1:100030088:     1   Min.   :-11.962   Min.   :-12.141   Min.   :-11.817  
   neg$chr1:100039873:     1   1st Qu.: -4.379   1st Qu.: -4.217   1st Qu.: -4.158  
   neg$chr1:10023338 :     1   Median : -2.807   Median : -2.663   Median : -2.585  
   neg$chr1:10024088 :     1   Mean   : -2.556   Mean   : -2.434   Mean   : -2.362  
   neg$chr1:10025009 :     1   3rd Qu.:  0.000   3rd Qu.:  0.000   3rd Qu.:  0.000  
   neg$chr1:10027750 :     1   Max.   : 17.708   Max.   : 16.162   Max.   : 16.210  
   (Other)           :113310                                                        
       W2.ir            W3.ir       
   Min.   :-12.194   Min.   :-11.880  
   1st Qu.: -3.078   1st Qu.: -4.087  
   Median : -1.000   Median : -2.711  
   Mean   : -1.577   Mean   : -2.370  
   3rd Qu.:  0.000   3rd Qu.:  0.000  
   Max.   : 17.562   Max.   : 16.711  
  </code></pre>
  
  <p>I am trying to plot cumulative probability using <code>ggplot</code> and <code>stat_ecdf</code>, </p>
  
  <p>using this code </p>
  
  <pre><code>ggplot(ir.m, aes(x=value)) + stat_ecdf(aes(group=variable,colour = variable))
  </code></pre>
  
  <p>Plot looks like this, </p>
  
  <p><a href="https://i.stack.imgur.com/AI3vc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AI3vc.png" alt="enter image description here"></a></p>
  
  <p>How do I obtain a smooth curve? Do I need to perform more statistical operation to obtain that?  </p>
  
  <h2>updated code</h2>
  
  <pre><code>ir.d = as.data.frame(ir.m)
  denss = split(ir.d, ir.d$variable) %&gt;%
    map_df(function(dw) {
      denss = density(dw$value, from=min(ir.d$value) - 0.05*diff(range(ir.d$value)), 
                     to=max(ir.d$value) + 0.05*diff(range(ir.d$value)))
      data.frame(x=denss$x, y=denss$y, cd=cumsum(denss$y)/sum(denss$y), group=dw$variable[1])
      head(denss)
    })
  summary(denss)
  &gt; summary(denss)
         x                 y                   cd               group    
   Min.   :-13.689   Min.   :0.0000000   Min.   :0.00000   N1.ir:512  
   1st Qu.: -5.466   1st Qu.:0.0000046   1st Qu.:0.07061   N2.ir:512  
   Median :  2.757   Median :0.0002487   Median :0.99552   W1.ir  :512  
   Mean   :  2.757   Mean   :0.0303942   Mean   :0.65315   W2.ir  :512  
   3rd Qu.: 10.980   3rd Qu.:0.0148074   3rd Qu.:0.99997   W3.ir  :512  
   Max.   : 19.203   Max.   :0.9440592   Max.   :1.00000
  </code></pre>
  
  <h1>plot</h1>
  
  <pre><code>ggplot() +
    stat_ecdf(data=ir.d, aes(x, colour=variable), alpha=0.8) +
    geom_line(data=denss, aes(x, cd, colour=group)) +
    theme_classic()
  </code></pre>
  
  <h1><a href="https://i.stack.imgur.com/Tlw59.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tlw59.png" alt="enter image description here"></a></h1>