3

I'm plotting a series of financial projections with ggplot's geom_path().

ggplot(df,aes(year,pot,color=iRate)) + geom_path() +  theme(legend.position="none") + ylim(0,300000)

This is what I have..[sorry it's a link]

problem graph

..two of the paths hit the right hand edge, year 40, but then nip back to their beginning. One line doesn't. This isn't a problem of axis-display limits, nor of a rogue line in the data frame - if I remove all years < 5 the same thing happens. It may be a just problem of overburdening the plotting device, but it is repeatable..

There are questions asking how to 'close' a geom_path which don't answer this. How do I ensure the path stays 'open'?

inflation <- seq(1, 1.1, 0.02)
potGrowth <- seq(1.02, 1.1, 0.02)
div <- 10000
initcapital <- 100000
output <- numeric()
lifespan <- 40
delay <- 10
for(j in 1:length(inflation)){ 
    str <- rep(0,lifespan)
    for(i in delay:lifespan){ str[i] <- floor((inflation[j]^i)*div) }
    for(k in 1:length(potGrowth)){ 
        cap <- initcapital
        for(i in 1:lifespan){  
        cap <- cap-str[i]; cap <- cap*potGrowth[k] 
        output <-  append(output, floor(cap))
        }
    }
}
iLen <- length(inflation); gLen <- length(potGrowth)
simulations <- iLen*gLen    
df <- data.frame(pot=output, projection=rep(1:simulations,each=lifespan), iRate=rep(inflation,each=lifespan*gLen), gRate=rep(rep(potGrowth,each=lifespan),times=iLen), year=rep(1:lifespan,times=simulations))

and here it is solved by inserting group=projection

ggplot(df,aes(year,pot,color=iRate,group=projection)) + geom_path() ...

non-problem graph

chris
  • 31
  • 1
  • 4
  • Can you use dput() to post a sample of your data? – Nick Becker Jul 21 '16 at 13:27
  • Try changing the ylim, I can see in the graph that it is stopping at ylim(0,300000). Increase it or just leave it blank. – Dinesh.hmn Jul 21 '16 at 13:30
  • `pot projection iRate gRate year bust` `157 694903 4 1 1.08 37 FALSE` `158 739695 4 1 1.08 38 FALSE` `159 788071 4 1 1.08 39 FALSE` `160 840316 4 1 1.08 40 FALSE` `161 110000 5 1 1.1 1 FALSE` `162 121000 5 1 1.1 2 FALSE` can't get the formatting to work! & Dinesh - no those ideas make no difference – chris Jul 21 '16 at 13:39
  • you should paste the output of `dput(df)` into your original question, not add code or data in comments. – hrbrmstr Jul 21 '16 at 13:52
  • @nick-becker pasted in all the code.. thanks for interest – chris Jul 21 '16 at 14:02
  • 2
    In the dataset you posted, a single value of `iRate` goes through each year sequence twice. So `geom_path` plots the first sequence of years and then has to go back to the beginning to plot the second sequence of years for the same `iRate`. Looks like this has something to do with `projection`, which you may want to incorporate in the graph somehow (maybe `group` by `projection`?). – aosmith Jul 21 '16 at 14:39
  • @aosmith well I have the 2 sets of variables which are growing [investment gains] or shrinking [inflation, drawdowns] the money - it's a pension pot - so it has to revisit one set repeatedly. & I end up with X*Y projections. It looks OK to me. .Anyway I'll try group_by. Thanks – chris Jul 21 '16 at 14:56
  • 1
    I plotted with the example dataset with your code, and it was having multiple sequences of years for a single `iRate` that was causing the lines to go back to the beginning (so `geom_path` was plotting the data correctly). If I added `group = projection` it didn't happen, although this may not make sense for whatever you want the plot to display. – aosmith Jul 21 '16 at 15:01
  • 1
    that's exactly what I want. I wasn't familiar with group=. Many thanks – chris Jul 21 '16 at 18:19

1 Answers1

5

Thanks @aosmith for mentioning the argument group in your comment. Here is a reproducible example describing the path closing issue with the airquality dataset. Let's say you want to plot a chart of temperature along the days of each month. Keeping all months on the same plot (in the spirit of these yearly plots: arcticseaicenews).

Using geom_path() alone leads to annoying "closing" lines between the last day of the previous month and the first day of the next month.

library(ggplot2)
ggplot(airquality, aes(x = Day, y = Temp)) +
    geom_path()

No group

Using geom_path() with the argument group=Month prevents these lines from appearing:

ggplot(airquality, aes(x = Day, y = Temp, group=Month)) +
    geom_path()

group

Of course you could also display months on different facets with facet_wrap depending on what you desire:

ggplot(airquality, aes(x = Day, y = Temp)) +
    geom_path() + 
    facet_wrap(~Month)
Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110
  • You saved my life, grouping is the solution!! – Euler_Salter May 02 '20 at 11:19
  • I have a situation where the main ggplot already has a `aes(col = VarA)` specification and `geom_path()` is just one element of the plot. I want to retain the colour grouping but also have a different grouping for the paths based on another variable. In this case even with `geom_path(aes(group = VarB, col = VarA))` I am getting the same issue of closing lines. – Karthik Thrikkadeeri Sep 03 '22 at 04:27
  • Found this solution that worked: https://stackoverflow.com/a/38503460/13000254 – Karthik Thrikkadeeri Sep 03 '22 at 04:37