Gantt chart using R

A post on creating a Gantt chart using R; these are good for showing a project timeline in grant applications. The code is adapted from an answer on Stack Overflow. You will need the ggplot2 and reshape2 packages; install them if you haven’t already.


Load libraries and clear workspace.

rm(list = ls())

Create your tasks as vectors in the format name, start date, and end date.

task1 <- c('Collect data', '2017-04-01', '2018-04-01')
task2 <- c('Clean data', '2018-04-01', '2018-06-01')
task3 <- c('Analyse data', '2018-06-01', '2019-04-01')
task4 <- c('Write report', '2019-04-01', '2020-04-01')

Create a data frame with appropriate classes.

# df <- = '^task\\d'), function(x) eval(parse(text = x)))), row.names = FALSE)

df <-, task2, task3, task4))
names(df) <- c('task', 'start', 'end')
df$task <- factor(df$task, levels = df$task)
df$start <- as.Date(df$start)
df$end <- as.Date(df$end)
df_melted <- melt(df, measure.vars = c('start', 'end'))

Create the plot.

# starting date to begin plot
start_date <- as.Date('2017-01-01')

ggplot(df_melted, aes(value, task)) + 
  geom_line(size = 10) +
  labs(x = '', y = '', title = 'Gantt chart using ggplot2') +
  theme_bw(base_size = 20) +
  theme(plot.title = element_text(hjust = 0.5),
        panel.grid.major.x = element_line(colour="black", linetype = "dashed"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text.x = element_text(angle = 0)) +
  scale_x_date(date_labels = "%Y %b", limits = c(start_date, NA), date_breaks = '1 year')

# see ?strptime if you want your date in a different format
# see if you want to adjust the x-axis

I used RStudio to adjust the dimensions of the plot. Data cleaning may actually take longer; these are just random examples.

Print Friendly, PDF & Email

Creative Commons License
This work is licensed under a Creative Commons
Attribution 4.0 International License

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.