在甘特图上添加阴影以描绘周末 [英] Add shading to a gantt chart to delineate weekends

查看:82
本文介绍了在甘特图上添加阴影以描绘周末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用ggplot创建了甘特图,代码如下:

I have created a gantt chart using ggplot, code below:

# load packages
require("ggplot2")
require("reshape2")

###############################################################################

# Create list of tasks name strings.
tasks <- c("Write introduction", "Parse citation data",
           "Construct data timeline",
           "Write methods", "Model formulation", 
           "Model selection", "Write results", "Write discussion",
           "Write abstract and editing")

# Compile dataframe of task names, and respective start and end dates.
dfr <- data.frame(
  name = factor(tasks, levels = tasks),
  start.date = as.Date(c("2018-04-09", "2018-04-09", "2018-04-16",
                         "2018-04-30", "2018-04-16", "2018-05-21",
                         "2018-06-04", "2018-07-02", "2018-07-30")),
  end.date = as.Date(c("2018-04-30", "2018-04-20", "2018-05-18",
                       "2018-06-01", "2018-05-18", "2018-06-01",
                       "2018-06-29", "2018-07-27", "2018-08-31"))
)

# Merge start and end dates into durations.
mdfr <- melt(dfr, measure.vars = c("start.date", "end.date"))

###############################################################################

# Create gantt chart.

ggplot(mdfr, aes(value, name)) +
  geom_line(size=4) +
  xlab(NULL) +
  ylab(NULL) +
  ggtitle("Project gantt chart") +
  theme_minimal() +
  theme(aspect.ratio = 0.3, axis.text = element_text(size = 10)) 

如何设置图表格式,以便在背景上加阴影或在背景网格上划定周末?

How would one go about formatting the chart so that weekends are shaded on the background or delineated on the background grid?

推荐答案

首先,您需要获取工作日信息:

First you need to get weekday information:

foo <- as.Date(mdfr$value)
# Generate days from first to last day
allDays <- seq(min(foo), max(foo), by = "days")
# Extract weekday
days <- data.frame(day = weekdays(allDays), date = allDays)

在周末添加 geom_vline (垂直线):

Add geom_vline (vertical lines) on weekends:

library(ggplot2)

ggplot(mdfr) +
    geom_vline(data = subset(days, day %in% c("Saturday", "Sunday")),
              aes(xintercept = date), color = "grey80", size = 2) +
    geom_line(aes(value, name), size = 4) +
    labs(title = "Project gantt chart",
         x = NULL,
         y = NULL) +
    theme_minimal() +
    theme(aspect.ratio = 0.3, 
          axis.text = element_text(size = 10))

这篇关于在甘特图上添加阴影以描绘周末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆