使用ggplot2和geom_area叠加负/正时间序列 [英] Stacked Negative/Positive Time Series Using ggplot2 and geom_area
问题描述
我试图重现一个堆叠的时间序列图,显示银行资产负债表的构成和规模如何随时间而变化。它应该看起来像这样:
资产超出x轴,负债低于x轴。
到目前为止,我已经能够使用 ggplot()
成功复制每个图的一半: p>
#plot assets stack
assets.plot< - ggplot(assetsm,aes(x = dates,y = value,填充=变量))+
geom_area()
#plot责任堆栈
liabiln.plot< - ggplot(负责人,aes(x =日期,y = value,fill = variable))+
geom_area()
p>
p>
但是当我把它们加在一起时,出现了一些问题:
#plot整体bs
bsplot < - ggplot(bsm,aes(x =日期,y =值,fill =变量))+
geom_area()
给出:
注意旁边的色标和上图,您可以看到:
- 只显示了一半变量(从V19开始)。
- 这些变量恰好与'负债'一半的数据一致(这些数据都应该是负数)。
在x上每个点的堆栈的总高度是相等的到上图中负载堆栈的总高度,但它不再从y = 0开始 - 它落在y轴的两侧。
我不知道我的代码中缺少的是什么导致这种情况 - 我使用
position =stack
显式地进行调整,以及尝试这个问题的答案(相同结果),我现在处于我的智慧结局。
我认为这可能是一个数据问题,所以我上传了数据 here 。如果我可以让问题更清楚或提供额外的细节,请告诉我。
解决方案我无法解释您目前看到的行为,但是当我做顶部/底部类型图像这些,我倾向于使用单独的数据框架单独调用一层:
ggplot()+
geom_area(data = datam,aes(x =日期,y =值,fill =变量))+
geom_area(data = liabilnm,aes(x = dates,y = value,fill = variable))
看起来像是在做什么:
< img src =https://i.stack.imgur.com/HTI8j.pngalt =在这里输入图片描述>
I'm trying to reproduce a stacked time-series graph which shows how the composition and size of a bank's balance sheet changes over time. It should look something like this:
Where assets go above the x-axis and liabilities go below it.
So far I've been able to reproduce each half of the graph successfully using
ggplot()
:# plot assets stack assets.plot <- ggplot(assetsm, aes(x=dates, y=value, fill=variable)) + geom_area() # plot liability stack liabiln.plot <- ggplot(liabilnm, aes(x=dates, y=value, fill=variable)) + geom_area()
which gives:
But when I add them together, something goes wrong:
# plot whole bs bs.plot <- ggplot(bsm, aes(x=dates, y=value, fill=variable)) + geom_area()
which gives:
Taking note of the colour scale beside it and the picture above, you can see that:
- Only half the variables are shown (from V19 onwards).
- These variables happen to coincide just with the 'liabilities' half of the data (which are all supposed to be negative numbers).
- The total height of the stack at each point on x is equal to the total height of the liabilities stack in the graph above, but it no longer starts at y=0 - it lands on both sides of the y-axis.
I have no idea what's missing from my code to cause this - I've fiddled around with making
position = "stack"
explicit, as well as trying the answer to this question (same result), and I'm now at my wit's end.I think this might be a data problem, so I've uploaded the data here. If I can make the question clearer or give extra details, let me know.
解决方案I can't quite explain the behavior you're seeing at the moment, but when I do top/bottom type plots like these, I tend to use separate data frames an separate layer calls:
ggplot() + geom_area(data = assetsm, aes(x=dates, y=value, fill=variable)) + geom_area(data = liabilnm, aes(x=dates, y=value, fill=variable))
which seems to look like what you're after:
这篇关于使用ggplot2和geom_area叠加负/正时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!