3
votes

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

1
Can you use dput() to post a sample of your data?Nick Becker
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
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 differencechris
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
that's exactly what I want. I wasn't familiar with group=. Many thankschris

1 Answers

4
votes

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)