如何删除堆叠的 geom_col 之间的空白

jak*_*kes 6 r ggplot2

library(tidyverse)
library(lubridate)

date <- seq(ymd('2018-08-01'), ymd('2018-08-31'), by = '1 day')
c <- 21.30
x1 <- runif(length(date), 0, 20)
x2 <- rnorm(length(date), 10, 3)
x3 <- abs(rnorm(length(date), 40, 10))
data <- data.frame(c, x1, x2, x3) %>% 
  t() %>% as.data.frame() %>% rownames_to_column('var')
data <- data %>%
  mutate(category1 = c('catA', 'catB', 'catB', 'catC') %>% as.factor(),
         category2 = c('catAA', 'catBA', 'catBB', 'catCA') %>% as.factor())
names(data) <- c('var', as.character(date), 'category1', 'category2')
data_long <- data %>% 
  gather(date, value, -var, -category1, -category2) %>% 
  mutate(date = ymd(date))

data_long %>%
  ggplot(aes(date, value, fill = category1)) +
  geom_col(position = 'stack') +
  scale_x_date(breaks = '1 week', date_labels = '%Y-%m-%d', expand = c(.01, .01)) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = .4)) +
  labs(fill = '')
Run Code Online (Sandbox Code Playgroud)

使用上面的示例数据和代码,我生成了以下图: 在此处输入图片说明

我需要做的是删除列之间的空格。我发现了一些类似的主题,但他们推荐使用position_dodge()while 它不能在我的情况下使用,因为我已经有了position = 'stack',无法替换。我怎样才能使列彼此相邻呢?

编辑

设置width = 1,正如@camille 所提出的,似乎可以处理原始数据,但不能汇总到数周或数月 - 请参阅下面的代码:

data_long %>%
  mutate(date = floor_date(date, unit = 'week', week_start = 1)) %>% 
  group_by(category1, date) %>% 
  summarise(value = sum(value, na.rm = TRUE)) %>% 
  ungroup() %>% 
  ggplot(aes(date, value, fill = category1, width = 1)) +
  geom_col(position = 'stack') +
  scale_x_date(breaks = '1 month', date_labels = '%Y-%m', expand = c(.01, .01)) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = .4)) +
  labs(fill = '')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

编辑 2。

正如@Camille 所指出的,在日期比例的情况下,宽度 1 可能指 1 天。但是,以下不会产生预期的输出并返回警告消息:position_stack requires non-overlapping x intervals

 data_long %>%
    mutate(date = floor_date(date, unit = 'month', week_start = 1)) %>% 
    group_by(category1, date) %>% 
    summarise(value = sum(value, na.rm = TRUE),
              n = n()) %>% 
    ungroup() %>% 
    ggplot(aes(date, value, fill = category1, width = n)) +
    geom_col(position = 'stack') +
    scale_x_date(breaks = '1 month', date_labels = '%Y-%m', expand = c(.01, .01)) +
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 90, vjust = .4)) +
    labs(fill = '')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

cam*_*lle 5

文档geom_col比我在上面的评论中更具体。width参数更详细的含义:

条宽。默认情况下,设置为数据分辨率的 90%。

在一般情况下,例如您的第一个案例,这可能仅表示一个离散案例与另一个案例之间的距离。但是对于具有真正分辨率的日期,这似乎是指天。我不确定是否有不同的方式来设置日期的分辨率,例如一个单位指的是一周,而不是一天。

我正在降低 alpha 只是为了能够查看条形是否重叠。

因此,如果不设置宽度,则默认为观察之间距离的 90%,即一周的 90%。

library(tidyverse)
library(lubridate)
...

summarized <- data_long %>%
  mutate(date = floor_date(date, unit = 'week', week_start = 1)) %>% 
  group_by(category1, date) %>% 
  summarise(value = sum(value, na.rm = TRUE)) %>% 
  ungroup()

ggplot(summarized, aes(date, value, fill = category1)) +
  geom_col(alpha = 0.6) +
  scale_x_date(breaks = '1 week', expand = c(.01, .01))
Run Code Online (Sandbox Code Playgroud)

将宽度设置为 1 表示宽度为 1 天。我觉得这里有一个其他人可能能够解释的差异,为什么这被视为 1 天而不是 100% 的分辨率。

ggplot(summarized, aes(date, value, fill = category1)) +
  geom_col(alpha = 0.6, width = 1) +
  scale_x_date(breaks = '1 week', expand = c(.01, .01))
Run Code Online (Sandbox Code Playgroud)

因此,要获得 1 周的宽度,也就是 7 天,请将宽度设置为 7。同样,我认为其他人可以在这里填写一些解释。

ggplot(summarized, aes(date, value, fill = category1)) +
  geom_col(alpha = 0.6, width = 7) +
  scale_x_date(breaks = '1 week', expand = c(.01, .01))
Run Code Online (Sandbox Code Playgroud)

编辑:根据我的评论中链接,最好的方法可能只是将日期转换为字符串,这样您就可以像往常一样在离散的 x 尺度上绘图。在调用 之前as.character,您可以进行任何您想要的格式设置。

summarized %>%
  mutate(date = as.character(date)) %>%
  ggplot(aes(x = date, y = value, fill = category1)) +
    geom_col(width = 1)
Run Code Online (Sandbox Code Playgroud)

  • 堆叠需要更多的数据准备,但您也可以考虑使用`geom_rect` 来完全避免处理宽度。然后它会等效地工作数天/数周/数月,甚至是不均匀间隔的时间段。 (2认同)