ggplot 在提供宽度时无法绘制时间序列

ggplot fails to plot time series when width is provided

我有以下 ggplot 时间序列数据

library(ggplot2)
dat = data.frame('date' = as.Date(c('2000-01-01', '2001-01-01', '2002-01-01', '2003-01-01', '2004-01-01', '2005-01-01', '2000-01-01', '2001-01-01', '2002-01-01', '2003-01-01', '2004-01-01', '2005-01-01')), 'val' = 1:12, 'sep' = rep(c('sep1', 'sep2'), each = 6))
ggplot(dat, aes(x = date, y = val)) +
geom_bar(aes(fill = sep), stat = 'identity', position = 'dodge', width = 0.8) +
scale_x_date(date_labels =  "%Y")

有了这个我可能会得到一些奇怪的情节

看起来,如果我不提供信息 width = 0.8,那么剧情似乎没问题。有什么方法可以调整每个条的宽度吗?

正如@Rajan 在评论中所说的那样,让你的宽度变大,你可以使用 date_breaks = "1 year" 来标记你的条形图,如下所示:

library(ggplot2)
dat = data.frame('date' = as.Date(c('2000-01-01', '2001-01-01', '2002-01-01', '2003-01-01', '2004-01-01', '2005-01-01', '2000-01-01', '2001-01-01', '2002-01-01', '2003-01-01', '2004-01-01', '2005-01-01')), 'val' = 1:12, 'sep' = rep(c('sep1', 'sep2'), each = 6))
ggplot(dat, aes(x = date, y = val)) +
  geom_bar(aes(fill = sep), stat = 'identity', position = 'dodge', width = 100) +
  scale_x_date(date_breaks = "1 year", date_labels =  "%Y") +
  labs(x = "Year")

输出: