title: How do I plot a running cumulative total from individual records in R?
body: <p>I have a dataset where different cities go in and out of a program, like this example dataset:</p>
  
  <pre><code>example.dat &lt;- data.frame (c(1000, 2000, 3000), c("15-10-01", "16-05-01", "16-07-01"), c("16-06-01", "16-10-01", "17-08-01"))
  colnames(example.dat) &lt;- c("Population", "Enter.Program", "Leave.Program")
  </code></pre>
  
  <p>This gives you a data frame that looks like:</p>
  
  <pre><code>Population  Enter.Program Leave.Program
  1000        15-10-01      16-06-01
  2000        16-05-01      16-10-01
  3000        16-07-01      17-08-01
  </code></pre>
  
  <p>First, I'd like to create an output table like this:</p>
  
  <pre><code>Per.Begin   Per.End     Total.Pop.In
  15-10-01    16-04-30    1000
  16-05-01    16-05-30    3000
  16-06-01    16-06-30    2000
  16-07-01    16-09-30    5000
  16-10-01    17-07-30    3000
  17-08-01    18-04-26    0 
  </code></pre>
  
  <p>And then plot this in ggplot as a graph that looks like either a step function or kind of like a jagged rectangular surface where the top edge is the running total, kind of like a cumulative density function but where the y-axis can go down as well as up, and where the x-axis goes in steps that are the width of time periods.</p>
  
  <p>Here are the steps I've blocked out, but I don't know how to execute:</p>
  
  <ol>
  <li>Make column of unique dates</li>
  <li>Make column of period change end dates (i.e., next unique date minus one day)</li>
  <li>Calculate running sum of the cities within each period (i.e., third column)</li>
  <li>Plot in ggplot</li>
  </ol>