Allen Tang
Allen Tang

Reputation: 11

Gantt Chart in R

I want to plot a scheduling diagram, in other words, Gantt Chart, in R.

What I have is a two dimensional array. A sample array is attache below.

:     1     2     3     4     5     6     7     8     9    10    11    12    :=
1    1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
2    0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
3    0.0   0.0   1.0   1.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
4    0.0   1.0   1.0   0.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
5    1.0   1.0   0.0   0.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
6    1.0   1.0   0.0   0.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
7    1.0   0.0   0.0   1.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
8    1.0   0.0   0.0   1.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
9    0.0   0.0   1.0   1.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
10   1.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0 
11   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
12   0.0   0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
13   0.0   0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
14   0.0   0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
15   0.0   0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
16   0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
17   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
18   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0   0.0
19   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0   0.0
20   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0   0.0
21   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0
22   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
23   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   0.0   1.0   0.0
24   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   1.0   0.0
25   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0
26   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
27   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   0.0   1.0
28   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0
29   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0
30   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
31   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
32   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0
33   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
34   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
35   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
36   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0

Each row denotes a time period, and each column denotes a project. If the number in the cell is 1, it means the current project is scheduled to run during this time period. There are 12 projects and 36 months. Is there any way to draw the gantt chart efficiently?

Upvotes: 0

Views: 833

Answers (2)

Steve de Peijper
Steve de Peijper

Reputation: 56

You could take a look at the following post. This uses ggplot as well.

https://dwh-businessintelligence.blogspot.nl/2016/05/what-if-for-project-management.html

gantt ggplot

What it basically does is draw the rectangles:

ggplot(dsg, aes(xmin = MinWeek, xmax = MaxWeek, ymin = ymin, ymax = ymin + 80 * (Hours/28), fill = factor(Person))) +
geom_rect() + 
facet_grid(Grouping~., scales = "free_y") + 
xlab("Week") +
theme(plot.title = element_text(lineheight=.8)) +
geom_text(aes(label = paste(Project, " (",Person, ", hrs:",Hours, ") WK:", MaxWeek , sep=""),  y = ymin + 24, x = MaxWeek - 2), size = 5) + 
geom_rect(data=dsg2, alpha=0.2) +
kobe_theme() + 
xlim(0,max(dsg[["MaxWeek"]]) + 2) +
geom_text(aes(label = Grouping,  y = 80, x = mw, size=20));

All the code is on github: https://github.com/stevedep/WhatIf

Upvotes: 0

lukeA
lukeA

Reputation: 54237

Well as a starter, here's a quick & dirty approach using ggplot:

gantt_ <- as.data.frame(t(gantt))
df <- do.call(rbind.data.frame, lapply(seq_along(gantt_), function(x) {
  r <- rle(gantt_[[x]])
  start <- cumsum(r$lengths) - r$lengths + 1
  end <- start + r$lengths - 1
  cbind(project = x, xmin = start[r$values == 1], xmax = end[r$values == 1])
}))
library(ggplot2)
ggplot(transform(df, 
                 project = factor(project, levels = 36:1),
                 xmin = xmin - .1,
                 xmax = xmax + .1), 
       aes(x = xmin, xend = xmax, y = project, yend = project)) + 
  geom_segment(size = 4) + 
  scale_x_continuous(breaks = 1:12) + labs(x = "time")

enter image description here

Data used:

gantt <- read.table(header=T, text="
1     2     3     4     5     6     7     8     9    10    11    12
1    1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
2    0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
3    0.0   0.0   1.0   1.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
4    0.0   1.0   1.0   0.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
5    1.0   1.0   0.0   0.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
6    1.0   1.0   0.0   0.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
7    1.0   0.0   0.0   1.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
8    1.0   0.0   0.0   1.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
9    0.0   0.0   1.0   1.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0
10   1.0   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0 
11   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
12   0.0   0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
13   0.0   0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
14   0.0   0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
15   0.0   0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
16   0.0   1.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
17   0.0   0.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
18   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0   0.0
19   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0   0.0
20   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0   0.0
21   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0
22   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0
23   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   0.0   1.0   0.0
24   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   1.0   0.0
25   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0
26   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
27   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   0.0   1.0
28   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0
29   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0
30   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
31   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
32   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   1.0   0.0   1.0
33   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
34   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
35   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0
36   0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0   0.0   1.0   0.0   1.0")

For more options, have a look at

library(sos)
findFn("gantt")

Upvotes: 1

Related Questions