首页 > 解决方案 > 使用 ggplot2 创建甘特图:scale_x_date 函数能否将日期显示为季度(例如 Q4 2020)?

问题描述

我正在使用 ggplot2 准备甘特图。我找到了一种最有用的方法,但我想进一步提高我的输出。

以下示例代码创建了一个数据框(这样设置,因为它相对容易动态编辑):

df <- as.data.frame(rbind(
    c("Category_1", "task_1", '2020-10-01', '2020-12-31'),
    c("Category_1", "task_2", '2021-01-01', '2021-04-01'),
    c("Category_2", "task_3", '2021-06-15', '2021-10-01'))) %>%
  select("Category" = 1, "task" = 2, "start" = 3, "end" = 4) %>%
  mutate(Category = (factor(Category, levels = (unique(Category)))),
         task = fct_rev(factor(task, levels = (task))),
         start = as.Date(start), 
         end = as.Date(end)) %>%
  mutate(lab_pos = start) %>%
  pivot_longer(3:4, names_to = "variable", values_to = "value")

然后此代码创建甘特图(为共享而简化):

ggplot(df, aes(value, task, color = Category)) + 
  geom_line(size = 14, show.legend = FALSE) +
  scale_x_date(date_labels = " %y %b", 
               limits = c(as.Date('2020-06-30'), as.Date('2021-12-31')), 
               date_breaks = '3 months', date_minor_breaks = "1 month", 
               position = "top") +
  labs(x = NULLy = NULL) +
  facet_grid(Category ~ ., space = "free_y", scales = "free_y", switch = "both")  

所以问题是如何以季度而不是日期显示由 scale_x_date 生成的 x 轴单位?例如,我更喜欢 Q1 21 而不是 1 月 21 日。有没有比 scale_x_date 更好的方法?

第二个问题是:如果我可以获得季度标签,我可以将该标签移动到季度的中间,以便主要网格线与季度接壤,标签位于它们之间,将整个区域标识为季度?

标签: rggplot2gantt-chart

解决方案


我们可以使用labels参数代替date_labels并传递自定义函数来从日期中提取季度信息。

library(ggplot2)

ggplot(df, aes(value, task, color = Category)) + 
  geom_line(size = 14, show.legend = FALSE) +
  scale_x_date(labels =  function(x) paste(quarters(x), format(x, '%Y')),
               limits = c(as.Date('2020-06-30'), as.Date('2021-12-31')), 
               date_breaks = '3 months', date_minor_breaks = "1 month", 
               position = "top") +
  labs(x = NULL,y = NULL) +
  facet_grid(Category ~ ., space = "free_y", scales = "free_y", switch = "both")

在此处输入图像描述


推荐阅读