开发者

Cashflow diagram in R?

A cashflow diagram is often used when explaining derivatives in financial engineering. It shows the payoffs at different times. I couldn't find a great example online, but it looks something like this:

Cashflow diagram in R?

I would like to make something roughly equivalent using ggplot2. My thought was to use a stacked bar plot, where the zero axis is somewhere in the middle. Does anyone know how to do this?

Here's some example data:

data.frame(time=c(1, 2, 3), positive=c(5, 0, 4), negative=c(-2, 0, 0))

Edit:

Thanks to Hadley's answer; the resulting image looks like:

Cashflow diagram in R?

With boxes it lo开发者_如何学运维oks like:

Cashflow diagram in R?


Here's one attempt.

ggplot(df, aes(time, xend = time)) + 
  geom_segment(aes(y = 0, yend = positive, colour = "positive"),
    position = "stack", arrow = arrow()) + 
  geom_segment(aes(y = 0, yend = negative, colour = "negative"), 
    position = "stack", arrow = arrow()) + 
  scale_colour_manual("Direction", 
    values = c("negative" = "red", "positive" = "black"))

But I think you really need to stack the values yourself, because you don't get quite enough control with ggplot2.


I suggested this to Khanh once for RQuantLib. This could now be your first patch :)

One issue, I think, is that you may not want full axis on either side -- long-dated zeros would have too little on the x-axis, and for standard bonds the different payout between coupons and par amount would likely look odd too.

Then again, this is R and fortune("yoda") still applies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜