R中的现金流量图?

时间:2010-08-25 16:07:22

标签: r ggplot2

在解释金融工程中的衍生工具时经常使用现金流图。它显示了不同时间的收益。我在网上找不到一个好的例子,但看起来像这样:

alt text

我想使用ggplot2大致相同的东西。我的想法是使用堆叠的bar plot,其中零轴位于中间的某个位置。有谁知道怎么做?

以下是一些示例数据:

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

编辑:

感谢哈德利的回答;生成的图像如下所示:

alt text

使用方框看起来像:

alt text

2 个答案:

答案 0 :(得分:2)

这是一次尝试。

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"))

但我认为你真的需要自己堆叠这些值,因为你没有用ggplot2获得足够的控制权。

答案 1 :(得分:1)

我向Khanh建议过RQuantLib一次。现在这可能是你的第一个补丁:)

我认为,有一个问题是你可能不希望任何一方都有完整的轴 - 长期的零点在x轴上会有太少,对于标准债券,优惠券和票面金额之间的差异可能会有所不同看起来很奇怪。

然后,这是R,fortune("yoda")仍然适用。