I used and modified the above example from Richie, worked like a charm. Modified version to show how his model could translate into ingesting CSV data rather than manually provided text items.
NOTE: Richie's answer was missing indication that 2 packages ( reshape and ggplot2 ) are needed for the above/below code to work.
rawschedule <- read.csv("sample.csv", header = TRUE) #modify the "sample.csv" to be the name of your file target. - Make sure you have headers of: Task, Start, Finish, Critical OR modify the below to reflect column count.
tasks <- c(t(rawschedule["Task"]))
dfr <- data.frame(
name = factor(tasks, levels = tasks),
start.date = c(rawschedule["Start"]),
end.date = c(rawschedule["Finish"]),
is.critical = c(rawschedule["Critical"]))
mdfr <- melt(dfr, measure.vars = c("Start", "Finish"))
#generates the plot
ggplot(mdfr, aes(as.Date(value, "%m/%d/%Y"), name, colour = Critical)) +
geom_line(size = 6) +
xlab("Duration") + ylab("Tasks") +
theme_bw()
There are now a few elegant ways to generate a Gantt chart in R.
Using Candela
library(candela)
data <- list(
list(name='Do this', level=1, start=0, end=5),
list(name='This part 1', level=2, start=0, end=3),
list(name='This part 2', level=2, start=3, end=5),
list(name='Then that', level=1, start=5, end=15),
list(name='That part 1', level=2, start=5, end=10),
list(name='That part 2', level=2, start=10, end=15))
candela('GanttChart',
data=data, label='name',
start='start', end='end', level='level',
width=700, height=200)
Using DiagrammeR
library(DiagrammeR)
mermaid("
gantt
dateFormat YYYY-MM-DD
title A Very Nice Gantt Diagram
section Basic Tasks
This is completed :done, first_1, 2014-01-06, 2014-01-08
This is active :active, first_2, 2014-01-09, 3d
Do this later : first_3, after first_2, 5d
Do this after that : first_4, after first_3, 5d
section Important Things
Completed, critical task :crit, done, import_1, 2014-01-06,24h
Also done, also critical :crit, done, import_2, after import_1, 2d
Doing this important task now :crit, active, import_3, after import_2, 3d
Next critical task :crit, import_4, after import_3, 5d
section The Extras
First extras :active, extras_1, after import_4, 3d
Second helping : extras_2, after extras_1, 20h
More of the extras : extras_3, after extras_1, 48h
")
Find this example and many more on DiagrammeRGitHub
If your data is stored in a data.frame, you can create the string to pass to mermaid() by converting it to the proper format.
Consider the following:
df <- data.frame(task = c("task1", "task2", "task3"),
status = c("done", "active", "crit"),
pos = c("first_1", "first_2", "first_3"),
start = c("2014-01-06", "2014-01-09", "after first_2"),
end = c("2014-01-08", "3d", "5d"))
# task status pos start end
#1 task1 done first_1 2014-01-06 2014-01-08
#2 task2 active first_2 2014-01-09 3d
#3 task3 crit first_3 after first_2 5d
Using dplyr and tidyr (or any of your favorite data wrangling ressources):
library(tidyr)
library(dplyr)
mermaid(
paste0(
# mermaid "header", each component separated with "\n" (line break)
"gantt", "\n",
"dateFormat YYYY-MM-DD", "\n",
"title A Very Nice Gantt Diagram", "\n",
# unite the first two columns (task & status) and separate them with ":"
# then, unite the other columns and separate them with ","
# this will create the required mermaid "body"
paste(df %>%
unite(i, task, status, sep = ":") %>%
unite(j, i, pos, start, end, sep = ",") %>%
.$j,
collapse = "\n"
), "\n"
)
)
As per mentioned by @GeorgeDontas in the comments, there is a little hack that could allow to change the labels of the x axis to dates instead of 'w.01, w.02'.
Assuming you saved the above mermaid graph in m, do:
timevis lets you create rich and fully interactive timeline
visualizations in R. Timelines can be included in Shiny apps and R
markdown documents, or viewed from the R console and RStudio Viewer.
I would like to improve the ggplot-Answer with several bars for each task.
First generate some data (dfrP is the data.frame of the other answer, dfrR is some other data.frame with realisation dates and mdfr is a merge fitting to the following ggplot()-statement):
Without the is.critical-information you could also use Plan/Real as color (which I would prefere), but I wanted to use the data.frame of the other answer to make it better comparable.
For me, Gvistimeline was the best tool to do this, but its required online connection was not useful to me. Thus I created a package called vistime that uses plotly (similar to the answer of @Steven Beaupré), so you can zoom in etc.:
Very old question, I know, but perhaps worth leaving here that - unsatisfied with the answers I found to this question - a few months ago I made a basic package for making ggplot2-based Gantt charts: ganttrify (more details in the package's readme).