Add shading to a gantt chart to delineate weekends

廉价感情. 提交于 2021-02-07 07:52:24

问题


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?


回答1:


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)

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))




回答2:


You can try

library(tidyverse)
mdfr %>% 
  as.tibble() %>% 
  mutate(date=as.POSIXct(value)) %>% 
  ggplot(aes(date, 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)) +
  scale_x_datetime(date_minor_breaks = "7 day") + 
  theme(panel.grid.minor.x = element_line(size=2, color = "pink"))

The idea is to transform value into POSIXct format (I'm using tidyverse here. But it is not necessary), then you can specify in scale_x_datetime minor breaks with "7 days. They start every Saturday. Using a bigger size will cover Sundays as well.



来源:https://stackoverflow.com/questions/49692707/add-shading-to-a-gantt-chart-to-delineate-weekends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!