0

I have this sample dataset in R:

enter image description here

What I would like to do is create a running total of the payment amount, separated into different series by the Rainforest.Segment factor. I have ordered the dataset by the dateofpayment

What I have is: ggplot(dataset, aes(x=dateofpayment, y=cumsum(PAYMENTAMOUNT), colour=Rainforest.Segment, group=Rainforest.Segment)) + geom_line()

I get what looks like the correct plot, but the cumulative sum is quite clearly incorrect (I know that the cumulative sum for each segment is < $50,000):

enter image description here

Could someone please give me some advice on what could be going wrong - and how to correct the issue?

Thanks so much!

JackReacher
  • 445
  • 4
  • 8
  • 19
  • 3
    Rather than posting images of data, please post a proper [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (leave out unnecessary columns). But your problem is that the aes is only evaulated once over the whole data.frame, it is not evaluated for each group. `ggplot` is not great for doing data transformations. You might consider using `dplyr` to transform your data first. With a reproducible example, it would be easier to give the exact code for this. – MrFlick Jan 12 '15 at 23:22
  • Thanks for the advice. Apologies for the formatting as I am rather new at this. – JackReacher Jan 13 '15 at 01:18

0 Answers0