r - 使用 R 绘制甘特图

标签 r charts gantt-chart

如何创建Gantt chart在 R 中?

我正在寻找一些复杂的东西(看起来或多或少像这样): this

附注我可以在没有依赖箭头的情况下生活。

最佳答案

现在有一些优雅的方法可以在 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)

enter image description here

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

enter image description here

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

#   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(或任何您最喜欢的数据整理资源):

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

来自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)

这给出:

enter image description here

<小时/>

使用情节

我偶然发现了这个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

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

关于r - 使用 R 绘制甘特图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3550341/

相关文章:

r - 如何返回 R 中两个 data.frames 之间匹配行的索引

javascript - 如何避免同一事件的重复执行?

excel - VBA Excel ActiveChart ProtectFormatting

visual-studio - 扩展 SSRS 中的图表功能

ios - ShinobiCharts - donut 系列绘图区

javascript - amcharts 甘特图轴恢复为时间而不是日期

javascript 甘特图 - 按天/小时/分钟绘制数据图表

r - 在 R 中将样方计数绘制为强度图

r - 在向量中查找字母数字元素

r - 引导生物等效性结果 R