title: Using ggplot to plot cumsum of two separate factors
body: <p>Using the following data I need to plot cumulative curves for factors "in" and "out.</p>
  
  <pre><code>years &lt;- c(1969, 1972, 1974, 1975, 1975, 1976, 1976, 1977, 1978, 1978, 1979, 1979, 1980, 1981, 1981, 1982, 1983, 1983, 1984, 1984, 1985, 1985, 1986, 1986, 1987, 1987, 1988, 1988, 1989, 1989, 1990, 1990, 1991, 1991, 1992)
  places &lt;- c("in","out","out","in","out","in","out","out","in","out","in",     "out","out","in","out","out","in","out","in","out","in","out","in", "out","in","out","in","out","in","out","in","out","in","out","in")   
  count &lt;- c(1,2,1,1,4,1,1,1,1,1,1,3,3,1,7,4,4,5,1,3,5,3,4,6,7,3,2,6,4,3,6,11,5, 7,9)
  
  peryear &lt;- data.frame(years,places,count)
  </code></pre>
  
  <p>If I plot using </p>
  
  <pre><code>ggplot(peryear %&gt;% filter(places=="in"),aes(x=years,y=cumsum(count))) + geom_point()+geom_line()
  ggplot(peryear %&gt;% filter(places=="out"),aes(x=years,y=cumsum(count))) + geom_point()+geom_line()
  </code></pre>
  
  <p>I get what I expected:</p>
  
  <p><a href="https://i.stack.imgur.com/ZfVi9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZfVi9.png" alt="cumsum of &quot;in&quot;"></a>
  <a href="https://i.stack.imgur.com/ii7XY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ii7XY.png" alt="cumsum of &quot;out&quot;"></a></p>
  
  <p>However, when I tried to use ggplot2 with</p>
  
  <pre><code>ggplot(peryear,aes(x=years,y=cumsum(count),color=places)) + geom_line()+geom_point()
  </code></pre>
  
  <p>I got the following wrong plot:</p>
  
  <p><a href="https://i.stack.imgur.com/KVswS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KVswS.png" alt="wrong cumsum ggplot"></a></p>
  
  <p>I believe this plot is wrong because the curves for both places are reaching values higher than 100 whereas for individual plots they get up to around 70. </p>
  
  <p>How can I use ggplot2 to plot cumsum curves of my data?</p>