使用 R 绘制甘特图 [英] Gantt charts with R

查看:24
本文介绍了使用 R 绘制甘特图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人用 R 创建过

使用 DiagrammeR

库(DiagrammeR)美人鱼("甘特图日期格式 YYYY-MM-DD一个非常好的甘特图部分基本任务完成 :done, first_1, 2014-01-06, 2014-01-08这是活动的 :active, first_2, 2014-01-09, 3d稍后再做:first_3,first_2 之后,5d在此之后执行此操作:first_4, after first_3, 5d部分重要事项已完成,关键任务 :crit, done, import_1, 2014-01-06,24h也完成了,也很关键 :crit, done, import_2, after import_1, 2d现在做这个重要的任务 :crit, active, import_3, after import_2, 3d下一个关键任务:crit,import_4,import_3 之后,5d附加部分第一个附加项 :active, extras_1, 在 import_4, 3d 之后第二个帮助:extras_2,在 extras_1 之后,20 小时更多的额外内容:extras_3,在 extras_1 之后,48 小时")

DiagrammeR GitHub 上查找此示例以及更多示例 <小时>

如果您的数据存储在 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"))# 任务状态 pos start end#1 task1 首先完成_1 2014-01-06 2014-01-08#2 task2 active first_2 2014-01-09 3d#3 task3 暴击 first_3 after first_2 5d

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

库(tidyr)图书馆(dplyr)美人鱼(粘贴0(# mermaid "header", 每个组件用 "
" 分隔(换行)"甘特图", "
","dateFormat YYYY-MM-DD", "
","title 一个非常好的甘特图", "
",# 合并前两列(任务和状态)并用:"分隔它们# 然后,合并其他列并用,"分隔它们# 这将创建所需的美人鱼身体"粘贴(df%>%unite(i, task, status, sep = ":") %>%unite(j, i, pos, start, end, sep = ",") %>%.$j,折叠 = "
"), "
"))

正如@GeorgeDontas 在评论中提到的,有一个

<小时>

使用时间可视化

来自 timevis

<小时>

使用情节

我偶然发现这个

然后您可以添加其他信息和注释、自定义字体和颜色等(详情请参阅博文)

Has anybody used R to create a Gantt chart?

P.S. I could live without the dependency arrows.

解决方案

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 DiagrammeR GitHub


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 "
" (line break)
    "gantt", "
", 
    "dateFormat  YYYY-MM-DD", "
", 
    "title A Very Nice Gantt Diagram", "
",
    # 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 = "
"
    ), "
"
  )
)

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:

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

Which gives:


Using timevis

From the timevis GitHub:

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.

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)

Which gives:


Using plotly

I stumbled upon this post providing another method using plotly. Here's an example:

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

Which gives:

You can then add additional information and annotations, customize fonts and colors, etc. (see blog post for details)

这篇关于使用 R 绘制甘特图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆