ggplot中的月缩放比例时,条宽度不一致 [英] Inconsistent bar widths when month-scaling in ggplot

查看:86
本文介绍了ggplot中的月缩放比例时,条宽度不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻找了一段时间,但找不到找到讨论类似问题的帖子。我在 ggplot 中使用日期缩放问题,我认为这与 ggplot 处理日期的方式有关。我试图摆脱列之间的所有空白,因为最终结果将类似于以下内容,这是一个容量规划图,显示了跨月的项目:

I looked for a while, and was unable to locate a post discussing a similar issue. I am having an issue with date-scaling in ggplot, which I think is related to the way ggplot is handling dates. I am trying to get rid of all of the white space in between the columns because my end result will be similar to the following, which is a capacity planning chart showing projects spanning the months:

即使使用默认的缩放比例和宽度参数,这些列也会出现不一致,这使我相信 ggplot 可能会以设置的宽度绘制所有列,但将它们放在x上-轴相对于每月的天数。但是,即使创建了一个月长度变量来独立缩放每一列,我也无法消除所有差距,而不会导致其他问题重叠。有人可以提供一些有关 ggplot 如何处理日期的见识吗?

The gap between the columns appears inconsistent even with default scaling and width parameters, which led me to believe that that ggplot may be drawing all of the columns at a set width but placing them on the x-axis relative to the number of days in each month. However, even after creating a "month length" variable to scale each column independently, I am unable to get all of the gaps to close, without causing others to overlap. Can someone provide some insight to how ggplot is handling dates?

以下是一些可重复使用的代码: / p>

Here is some reproducible code to play with:

library(lubridate)
library(ggplot2)
library(scales)

Months <- c("2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01", "2015-05-01", 
            "2015-06-01", "2015-07-01", "2015-08-01", "2015-09-01", "2015-10-01")
Months <- as.Date(Months)
Totals <- c(1330, 1010, 950, 1110, 1020, 1160, 1320, 880, 620, 320)
df <- data.frame(Months, Totals)
df$MonthLength <- days_in_month(as.Date(df$Month))

ggplot()+
  geom_bar(data=df, aes (x = Months, y = Totals, fill = Months, width = MonthLength), 
                    position = "dodge", stat = "identity", alpha = 0.80)+
  coord_cartesian(ylim = c(0, 1600))+
  scale_x_date(breaks="1 month",limits=c(as.Date("2015-01-01"),as.Date("2015-10-01")), 
                                labels = date_format("%b-%Y"))


推荐答案

我认为正在发生的事情是ggplot在每个月的15号放置了每个刻度线,因此放置了每个小节。因此,如果您将条形图放大,则二月形图条与进行曲重叠,但不到一月。 (我会称它为错误,但我是谁;-))

I think what is happening, is that ggplot puts each tick mark and hence each bar at the 15th of the month. Therefore, if you make the bars larger, the february-bar overlaps with march, but falls short of january. (I would call that a bug, but who am I ;-))

解决此问题的方法是将您的月份转换为因子,然后将 width = 1

The way around this, is the convert your months to a factor, and then set width=1:

df$Fmonth <- factor(month(Months))
ggplot(data=df, aes (x = Fmonth, y = Totals, fill = Months)) +
  geom_bar(position = "dodge", stat = "identity", alpha = 0.80, width=1) +
  coord_cartesian(ylim = c(0, 1600))

< a href = https://i.stack.imgur.com/JTUrq.png rel = noreferrer>

另一种解决方法可能是手动计算月份的中旬,将数据放在该位置,然后按照原始格式条宽的路线= MonthLength。

Another way around it might be to calculate the middle of the month by hand, position your data there and then follow your original route of bar width=MonthLength.

这篇关于ggplot中的月缩放比例时,条宽度不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆