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]
..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() ...
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 – chrisiRate
goes through each year sequence twice. Sogeom_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 sameiRate
. Looks like this has something to do withprojection
, which you may want to incorporate in the graph somehow (maybegroup
byprojection
?). – aosmith