2010-08-23 322 views
58

有沒有人使用R來創建一個Gantt chart? 我知道的唯一解決方案是this,但如果可能的話,我正在尋找更復雜的東西(看起來或多或少像thisthis)。甘特圖與R

P.S.我可以生活在沒有依賴箭頭的地方。

回答

62

現在有幾個優雅的方式來產生R.

甘特圖使用製圖

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

enter image description here

找到這個例子中,還有更多的DiagrammeRGitHub


如果您的數據存儲在data.frame中,則可以通過將其轉換爲正確的格式創建要傳遞到mermaid()的字符串。

考慮以下幾點:

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 

使用dplyrtidyr(或任何你喜歡的數據扯皮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" 
) 
) 

按照通過在註釋中提到@GeorgeDontas,有一個little hack,它可以允許將x軸的標籤更改爲日期而不是'w.01,w.02'。

假設你保存上述美人魚圖中m,做到:

m$x$config = list(ganttConfig = list(
    axisFormatter = list(list(
    "%b %d, %Y" 
    ,htmlwidgets::JS(
     'function(d){ return d.getDay() == 1 }' 
    ) 
)) 
)) 

其中給出:

enter image description here


使用timevis

從在timevisGitHub

timevis可以讓你創建豐富和充分互動時間表 在R.時間表可視化可以包含在閃亮的應用程序和R 降價文件,或者從R控制檯和RStudio查看器中查看。

library(timevis) 

data <- data.frame(
    id  = 1:4, 
    content = c("Item one" , "Item two" ,"Ranged item", "Item four"), 
    start = c("2016-01-10", "2016-01-11", "2016-01-20", "2016-02-14 15:00:00"), 
    end  = c(NA   ,   NA, "2016-02-04", NA) 
) 

timevis(data) 

其中給出:

enter image description here


使用plotly

我偶然發現了這個post提供使用plotly另一種方法。這裏有一個例子:

library(plotly) 

df <- read.csv("https://cdn.rawgit.com/plotly/datasets/master/GanttChart-updated.csv", 
       stringsAsFactors = F) 

df$Start <- as.Date(df$Start, format = "%m/%d/%Y") 
client <- "Sample Client" 
cols  <- RColorBrewer::brewer.pal(length(unique(df$Resource)), name = "Set3") 
df$color <- factor(df$Resource, labels = cols) 

p <- plot_ly() 
for(i in 1:(nrow(df) - 1)){ 
    p <- add_trace(p, 
       x = c(df$Start[i], df$Start[i] + df$Duration[i]), 
       y = c(i, i), 
       mode = "lines", 
       line = list(color = df$color[i], width = 20), 
       showlegend = F, 
       hoverinfo = "text", 
       text = paste("Task: ", df$Task[i], "<br>", 
           "Duration: ", df$Duration[i], "days<br>", 
           "Resource: ", df$Resource[i]), 
       evaluate = T 
) 
} 

p 

其中給出:

enter image description here

然後,您可以添加額外的信息和說明,自定義字體和顏色等(見博客文章的詳細信息)

+0

確實不錯。然而,在我看來,使用存儲在數據框中的數據自動創建傳遞給美人魚的字符串非常困難。 – 2015-05-02 06:56:58

+0

是否可以將日期顯示爲x軸標籤,而不是「w.01」,「w.02」等? – 2015-05-02 07:38:48

+0

@GeorgeDontas其實很簡單,看更新。 – 2015-05-04 00:27:41

4

Here's a post我寫的使用ggplot生成類似甘特圖的東西。不是很複雜,但可能會給你一些想法。

+0

謝謝,那是真正有用的 – slackline 2014-01-24 08:23:59

26

簡單的ggplot2甘特圖。

首先,我們創建一些數據。

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) 
+0

我只能兩次創造一些數據:-) – 2010-08-24 11:52:21

+0

@ gd047:說了兩手捂臉調用。白癡現在修復了。 – 2010-08-24 12:09:26

+1

這是非常好的,但我最主要的是爲每個任務顯示多個欄的方式(正如你在我給出的例子中看到的),例如一個用於基線,另一個用於實際任務持續時間。有沒有辦法做這樣的事情? – 2010-08-24 18:29:01

7

試試這個:

install.packages("plotrix") 
library(plotrix) 
?gantt.chart 
6

包​​支持燃盡圖和甘特圖 的創建,幷包含一個plot.gantt功能。請參見this R Graphical Manual page

另請參閱如何使用Plotly's R API GANTT CHARTS IN R USING PLOTLY在R中創建一個。

+0

這工作得很好,很容易適應。我已經寫了一個小函數來添加跨越時間的事件:https://gist.github.com/crsh/4f9ce67f408611bc3974 – crsh 2015-05-07 16:04:46

3

我使用並修改了Richie上面的例子,像一個魅力一樣工作。修改版本顯示他的模型如何轉化爲攝取CSV數據,而不是手動提供文本項目。

:Richie的答案是缺少跡象表明,2包(重塑GGPLOT2)需要爲高於/低於代碼工作。

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() 
1

PlotPrjNetworks爲項目管理提供了有用的網絡工具。

library(PlotPrjNetworks) 
project1=data.frame(
task=c("Market Research","Concept Development","Viability Test", 
"Preliminary Design","Process Design","Prototyping","Market Testing","Final Design", 
"Launching"), 
start=c("2015-07-05","2015-07-05","2015-08-05","2015-10-05","2015-10-05","2016-02-18", 
"2016-03-18","2016-05-18","2016-07-18"), 
end=c("2015-08-05","2015-08-05","2015-10-05","2016-01-05","2016-02-18","2016-03-18", 
"2016-05-18","2016-07-18","2016-09-18")) 
project2=data.frame(
from=c(1,2,3,4,5,6,7,8), 
to=c(2,3,4,5,6,7,8,9), 
type=c("SS","FS","FS","SS","FS","FS","FS","FS"), 
delay=c(7,7,7,8,10,10,10,10)) 
GanttChart(project1,project2) 

enter image description here

5

您可以用GoogleVis package做到這一點:

datTL <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)), 
        Name=c("Washington", "Adams", "Jefferson", 
          "Adams", "Jefferson", "Burr"), 
        start=as.Date(x=rep(c("1789-03-29", "1797-02-03", 
              "1801-02-03"),2)), 
        end=as.Date(x=rep(c("1797-02-03", "1801-02-03", 
             "1809-02-03"),2))) 

Timeline <- gvisTimeline(data=datTL, 
         rowlabel="Name", 
         barlabel="Position", 
         start="start", 
         end="end", 
         options=list(timeline="{groupByRowLabel:false}", 
             backgroundColor='#ffd', 
             height=350, 
             colors="['#cbb69d', '#603913', '#c69c6e']")) 
plot(Timeline) 

enter image description here

來源:https://cran.r-project.org/web/packages/googleVis/vignettes/googleVis_examples.html

1

我想提高ggplot應答與幾個酒吧爲每個任務。

首先生成一些數據(DFRP是對方的回答的data.frame,dfrR是一些其他的data.frame與實現日期和mdfr被合併入以下ggplot() - 語句):

library(reshape2) 
tasks <- c("Review literature", "Mung data", "Stats analysis", "Write Report") 
dfrP <- 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) 
) 
dfrR <- data.frame(
    name  = factor(tasks, levels = tasks), 
    start.date = as.Date(c("2010-08-22", "2010-10-10", "2010-11-01", NA)), 
    end.date = as.Date(c("2010-11-03", "2010-12-22", "2011-02-24", NA)), 
    is.critical = c(TRUE, FALSE, FALSE,TRUE) 
) 
mdfr <- merge(data.frame(type="Plan", melt(dfrP, measure.vars = c("start.date", "end.date"))), 
    data.frame(type="Real", melt(dfrR, measure.vars = c("start.date", "end.date"))), all=T) 

現在,使用方面的任務名稱繪製這樣的數據:

library(ggplot2) 
ggplot(mdfr, aes(x=value, y=type, color=is.critical))+ 
    geom_line(size=6)+ 
    facet_grid(name ~ .) + 
    scale_y_discrete(limits=c("Real", "Plan")) + 
    xlab(NULL) + ylab(NULL) 

沒有is.critical信息,你也可以使用計劃/實色(我會prefere),但我想用data.frame的另一個答案,使其更好的可比性。

0

對我來說,Gvistimeline是做到這一點的最佳工具,但它需要在線連接是不是對我很有用。因此,我創建了一個包卡列斯vistime使用plotly(類似於@StevenBeaupré的的答案),所以你可以放大等:

https://github.com/shosaco/vistime

vistime:創建使用plotly互動時間表或甘特圖.js文件。 圖表可以包含在Shiny應用程序中,並通過 plotly_build()進行操作。

install.packages("vistime")  
dat <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)), 
       Name = c("Washington", "Adams", "Jefferson", "Adams", "Jefferson", "Burr"), 
       start = rep(c("1789-03-29", "1797-02-03", "1801-02-03"), 2), 
       end = rep(c("1797-02-03", "1801-02-03", "1809-02-03"), 2), 
       color = c('#cbb69d', '#603913', '#c69c6e'), 
       fontcolor = rep("white", 3)) 

vistime(dat, events="Position", groups="Name", title="Presidents of the USA") 

enter image description here

4

考慮使用package projmanr(CRAN上發佈了0.1.0版本的2017年8月23日)。

library(projmanr) 

# Use raw example data 
(data <- taskdata1) 

taskdata1

id name duration pred 
1 1 T1  3  
2 2 T2  4 1 
3 3 T3  2 1 
4 4 T4  5 2 
5 5 T5  1 3 
6 6 T6  2 3 
7 7 T7  4 4,5 
8 8 T8  3 6,7 

現在開始準備甘特圖:

# Create a gantt chart using the raw data 
gantt(data) 

enter image description here

# Create a second gantt chart using the processed data 
res <- critical_path(data) 
gantt(res) 

enter image description here

# Use raw example data 
data <- taskdata1 
# Create a network diagram chart using the raw data 
network_diagram(data) 

enter image description here

# Create a second network diagram using the processed data 
res <- critical_path(data) 
network_diagram(res) 

enter image description here