r - R中的现金流量图?

标签 r ggplot2

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

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

最佳答案

这是一种尝试。

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 进行足够的控制。

关于r - R中的现金流量图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3567941/

相关文章:

r - 基于R中数值的着色图

r - 在 ggplot2 中覆盖多个 geom_tile 图

r - ggplot2,排列多个图,大小相同,之间没有间隙

根据因子级别重新格式化标签/保留 ggplot2::facet_wrap() 中多因子构面的顺序

r - 如何(更容易)在 ggplot2 中创建漂亮的 x 轴刻度(即 pi/2, pi, 3pi/2, ...)?

r - sweave 和 ggplot2 : no pdfs generated at all

r - 前两个之间的字符串 (.dots)

r - ggplot2 错误 - 'Discrete value supplied to continuous scale'

r - 有没有办法使用 ggplot 创建一个 "star"绘图?

r - 如何使用基本 R 绑定(bind)列表中的每个第 n 个元素?