【问题标题】:Creating Gantt charts with ggplot2: can the scale_x_date function display dates as quarters (eg Q4 2020)?使用 ggplot2 创建甘特图:scale_x_date 函数能否将日期显示为季度(例如 Q4 2020)?
【发布时间】:2020-09-26 00:42:31
【问题描述】:

我正在使用 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 更好的方法?

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

【问题讨论】:

    标签: r ggplot2 gantt-chart


    【解决方案1】:

    我们可以使用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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多