堆叠条形图,X轴上的日期放置得很好。

问题描述 投票:0回答:1

我试图建立一个堆叠的Barplot,在x-Ais上的条形图和Dates之间没有空隙.为了实现这一点,我将宽度设置为以下日期之间的差异,并将x的位置n预算为dateifference的一半.因此,如果日期是2018-01-31,我希望条形图正好从该日期开始,并在下一个日期(2018-02-28)结束。到目前为止,这很好,但当引入该定位逻辑时,我的条形图停止了堆叠(图片:条形图是重叠的,但不是堆叠的->将和为1)。我如何才能重新堆叠它们?enter image description here

ggplot(weights_gg2, aes(x=Date, y=Exposure, fill=Bond)) +
  geom_bar(stat="identity", colour="white", width=rep(c(diff(weights_df$Date), 20), 13), position = position_nudge(x=rep(c(diff(weights_df$Date), 20), 13)/2)) +
  theme_bw(base_size=12)

需要的数据:weights_df包含了所有数据和日期,而weights_gg2已经被ggplot融化了。

r ggplot2 geom-bar stacked-chart
1个回答
0
投票

darios的答案是最准确的问题。

操作输入数据,而不是改变位置后。

hjust <- rep(c(diff(weights_df$Date), 20), 13)/2
ggplot(weights_gg2, aes(x=Date+hjust, y=Exposure, fill=Bond)) +
  geom_bar(stat="identity", colour="white", width=rep(c(diff(weights_df$Date), 20), 13))+
  theme_bw(base_size=12)
© www.soinside.com 2019 - 2024. All rights reserved.