堆叠条形图,在 x 轴上放置正确的日期
Stacked Barplot with well placed Dates on x-Axis
我正在尝试构建一个堆叠条形图,在 x 轴上的条形图和日期之间没有间隙。
为此,我将宽度设置为以下日期之间的差异,并将 x 位置微调日期差异的一半。
因此,如果日期是 2018-01-31,我希望栏正好在该日期开始并在下一个日期 (2018-02-28) 结束。到目前为止效果很好,但是在引入该定位逻辑时,我的条形图不再堆叠(图片:条形图重叠但未堆叠 -> 总和为 1)。我怎样才能重新堆叠它们?
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
融化
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)
我正在尝试构建一个堆叠条形图,在 x 轴上的条形图和日期之间没有间隙。
为此,我将宽度设置为以下日期之间的差异,并将 x 位置微调日期差异的一半。
因此,如果日期是 2018-01-31,我希望栏正好在该日期开始并在下一个日期 (2018-02-28) 结束。到目前为止效果很好,但是在引入该定位逻辑时,我的条形图不再堆叠(图片:条形图重叠但未堆叠 -> 总和为 1)。我怎样才能重新堆叠它们?
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
融化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)