0

Below is a table containing the timeline of the construction of three buildings. The timelines include the start date, completion of each process - foundation, structure, and interior.

How can we create a gantt chart using ggplot showing the timeline of the construction of all buildings?

library(tidyverse)
library(lubridate)


tbl <- tibble(
  buildings = c("A", "B", "C"),
  start = ymd(c("2017-12-01", "2018-02-01", "2018-05-01")),
  foundation = ymd(c("2018-01-01", "2018-04-01", "2018-06-01")),
  structure = ymd(c("2018-02-09", "2018-05-01", "2018-07-01")),
  interior = ymd(c("2018-05-01", "2018-06-01", "2018-07-24"))
)

Below is a simple example to create a Gantt chart taken from here. The above question will require some manipulation of data. We may have to create tasks such as - start_to_foundation

library(reshape2)
library(ggplot2)

tasks <- c("Review literature", "Mung data", "Stats analysis", "Write Report")
dfr <- data.frame(
  name        = factor(tasks, levels = tasks),
  start.date  = as.Date(c("2010-08-24", "2010-10-01", "2010-11-01", "2011-02-14")),
  end.date    = as.Date(c("2010-10-31", "2010-12-14", "2011-02-28", "2011-04-30")),
  is.critical = c(TRUE, FALSE, FALSE, TRUE)
)
mdfr <- melt(dfr, measure.vars = c("start.date", "end.date"))

ggplot(mdfr, aes(value, name, colour = is.critical)) + 
  geom_line(size = 6) +
  xlab(NULL) + 
  ylab(NULL)

afik ziv
  • 7
  • 3
SiH
  • 1,378
  • 4
  • 18

1 Answers1

1

See if this meets your need. Each line needs two points. I made the end points one day before the next stage. The final end dates were set to 30 days after the last stage.

ltbl =tbl %>% mutate(start2=foundation-1, 
                 foundation2=structure-1, 
                 structure2=interior-1,
                 interior2=interior+30) %>% 
      pivot_longer(2:9, names_to = "period", values_to = "date") %>%
      mutate(stage=str_extract(period,"[a-z]+"))

ggplot(ltbl, aes(date, buildings, colour = stage)) + 
  geom_line(size = 6) +xlab(NULL) + ylab(NULL)
Bing
  • 1,083
  • 1
  • 10
  • 20