甘特图与R

Geo*_*tas 80 charts r gantt-chart

有没有人用R来创建甘特图?我所知道的唯一解决方案是这个,但我正在寻找更复杂的东西,如果可能的话(看起来或多或少像这样或者这样).

PS我可以没有依赖箭头.

Ste*_*pré 91

现在有一些优雅的方法可以在R中生成甘特图.

使用坎德拉

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

使用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
")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

GitHub上找到这个例子以及更多DiagrammeR


如果您的数据存储在a中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
Run Code Online (Sandbox Code Playgroud)

使用dplyrtidyr(或任何您喜欢的数据争夺资源):

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"
  )
)
Run Code Online (Sandbox Code Playgroud)

正如@GeorgeDontas在评论中提到的那样,有一个小的黑客可以允许将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 }' 
    )
  ))
))
Run Code Online (Sandbox Code Playgroud)

这使:

在此输入图像描述


使用timevis

来自timevis GitHub:

timevis允许您在R中创建丰富且完全交互式的时间轴可视化.时间轴可以包含在Shiny应用程序和R降价文档中,也可以从R控制台和RStudio Viewer中查看.

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)
Run Code Online (Sandbox Code Playgroud)

这使:

在此输入图像描述


用剧情

我偶然发现这篇文章提供了另一种方法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
Run Code Online (Sandbox Code Playgroud)

这使:

在此输入图像描述

然后,您可以添加其他信息和注释,自定义字体和颜色等.(有关详细信息,请参阅博客文章)

  • 覆盖甘特图以允许自定义日期轴而不是0-52周比例:https://github.com/rich-iannone/DiagrammeR/issues/77 (2认同)

Ric*_*ton 29

简单的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"))
Run Code Online (Sandbox Code Playgroud)

现在画出情节.

ggplot(mdfr, aes(value, name, colour = is.critical)) + 
  geom_line(size = 6) +
  xlab(NULL) + 
  ylab(NULL)
Run Code Online (Sandbox Code Playgroud)


gio*_*mai 10

很老的问题,我知道,但也许值得留在这里 - 对我找到的这个问题的答案不满意 - 几个月前我制作了一个用于制作基于 ggplot2 的甘特图的基本包:ganttrify(更多细节在包的自述文件中) .

示例输出: 在此处输入图片说明

  • 这是一个很棒的包,感谢您提供它! (3认同)

use*_*503 9

考虑使用该软件包projmanr(2017年8月23日在CRAN上发布的0.1.0版).

library(projmanr)

# Use raw example data
(data <- taskdata1)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

现在开始准备甘特图:

# Create a gantt chart using the raw data
gantt(data)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

# Create a second gantt chart using the processed data
res <- critical_path(data)
gantt(res)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

# Use raw example data
data <- taskdata1
# Create a network diagram chart using the raw data
network_diagram(data)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

# Create a second network diagram using the processed data
res <- critical_path(data)
network_diagram(res)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


juu*_*uur 7

试试这个:

install.packages("plotrix")
library(plotrix)
?gantt.chart
Run Code Online (Sandbox Code Playgroud)


Geo*_*tas 7

Package plan支持创建燃尽图和甘特图并包含一个plot.gantt功能.请参见此R图形手册页面

另请参阅如何在R中使用Plotly的R API GANTT CHARTS在R中使用绘图制作一个.


sho*_*aco 6

对我来说,Gvistimeline 是执行此操作的最佳工具,但其所需的在线连接对我来说没有用。vistime因此,我创建了一个名为use 的包plotly(类似于 @Steven Beaupr\xc3\xa9 的答案),因此您可以放大等:

\n\n

https://github.com/shosaco/vistime

\n\n
\n

vistime:使用plotly.js 创建交互式时间线或甘特图。图表可以包含在 Shiny 应用程序中并通过plotly_build() 进行操作。

\n
\n\n
install.packages("vistime")    \nlibrary("vistime")  \n\ndat <- data.frame(Position=c(rep("President", 3), rep("Vice", 3)),\n              Name = c("Washington", "Adams", "Jefferson", "Adams", "Jefferson", "Burr"),\n              start = rep(c("1789-03-29", "1797-02-03", "1801-02-03"), 2),\n              end = rep(c("1797-02-03", "1801-02-03", "1809-02-03"), 2),\n              color = c(\'#cbb69d\', \'#603913\', \'#c69c6e\'),\n              fontcolor = rep("white", 3))\n\nvistime(dat, events="Position", groups="Name", title="Presidents of the USA")\n
Run Code Online (Sandbox Code Playgroud)\n\n

在此输入图像描述

\n


von*_*njd 5

您可以使用GoogleVis套餐执行此操作:

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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