简体   繁体   English

R中的甘特图

[英]Gantt Chart in R

I want to plot a scheduling diagram, in other words, Gantt Chart, in R. 我想在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. 如果单元格中的数字为1,则表示当前项目计划在此时间段内运行。 There are 12 projects and 36 months. 有12个项目,历时36个月。 Is there any way to draw the gantt chart efficiently? 有什么方法可以有效地绘制甘特图吗?

Well as a starter, here's a quick & dirty approach using ggplot: 首先,这是使用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")

在此处输入图片说明

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

You could take a look at the following post. 您可以看看以下帖子。 This uses ggplot as well. 这也使用ggplot。

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

甘特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 所有代码都在github上: https : //github.com/stevedep/WhatIf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM