R 中的现金流量图?

发布于 2024-09-15 23:34:05 字数 609 浏览 1 评论 0原文

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

alt text

我想做一些粗略的东西等价于使用ggplot2。我的想法是使用堆叠的 条形图,其中零轴位于中间。有谁知道该怎么做?

以下是一些示例数据:

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

编辑:

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

alt text

带框的图像如下所示:

替代文本

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:

alt text

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:

alt text

With boxes it looks like:

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你如我软肋 2024-09-22 23:34:05

这是一次尝试。

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 获得足够的控制。

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.

三五鸿雁 2024-09-22 23:34:05

我曾经向 Khanh 推荐过这个 RQuantLib。现在这可能是的第一个补丁:)

我认为,有一个问题是你可能不希望任一侧都有完整的轴——长期的零在x轴上会太少,并且对于标准债券,息票和票面金额之间的不同支付也可能看起来很奇怪。

话又说回来,这是 R,并且 fortune("yoda") 仍然适用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文