r - 使用 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 更好的方法?
第二个问题是:如果我可以获得季度标签,我可以将该标签移动到季度的中间,以便主要网格线与季度接壤,标签位于它们之间,将整个区域标识为季度?
解决方案
我们可以使用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")
推荐阅读
- python - Tensorflow no module named '_pywrap_tensorflow_internal' (DLL's appear to be fine)
- c# - Logic operations in string value which include symbols
- r - ggseasonplot from forecast package does not recognize ts object
- ember.js - 使用 ember-bootstrap 操作的正确方法
- matlab - 如何在 Octave 中解析具有空值的 CSV 文件?
- swift - 函数外的 Swift 作用域
- arrays - 如何让整数方法“猜测”作为数组进入另一个方法?
- laravel - 具有条件默认值的 Laravel old() 指令
- amazon-web-services - 运行 AWS ECS 容器时 ELB 运行状况检查失败
- python - 如何标准化数据框中的列,然后绘制回归线?