简体   繁体   English

堆叠条形图,在 x 轴上放置良好的日期

[英]Stacked Barplot with well placed Dates on x-Axis

I am trying to build a stacked Barplot, without gaps between the bars and Dates on the x-Axis.我正在尝试构建一个堆叠的条形图,在 x 轴上的条形和日期之间没有间隙。 To achieve this, I set the width to the difference between the following dates and nudget the x position by half the datedifference.为了实现这一点,我将宽度设置为以下日期之间的差异,并将 x 位置微调为日期差异的一半。 So if the Date is 2018-01-31, I want the bar to start exactly at that date and end at the next date (2018-02-28).因此,如果日期是 2018 年 1 月 31 日,我希望条形图恰好在该日期开始并在下一个日期 (2018 年 2 月 28 日) 结束。 So far that works well, but when introducing that positioning logic, my bars stopped being stacked (picture: bars are overlapping but not stacked -> would sum up to 1).到目前为止效果很好,但是当引入该定位逻辑时,我的条形停止堆叠(图片:条形重叠但未堆叠 -> 总和为 1)。 How can I restack them?我怎样才能重新堆叠它们? 在此处输入图片说明

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)

Needed data: weights_df contains all data and the dates, while weights_gg2 is already melted for ggplot需要的数据: weights_df 包含所有数据和日期,而 weights_gg2 已经为 ggplot 融化

darios answer was the most accurate to the problem: darios 的回答是最准确的问题:

Manipulate input data rather than changing the position afterwards:操作输入数据而不是之后改变位置:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM