【问题标题】:R visualization - creating a gantt chartR 可视化 - 创建甘特图
【发布时间】:2020-12-19 07:48:34
【问题描述】:

我目前正在使用提供的数据进行项目:

TASK_ID START_TIME   STOP_TIME     PERSON_ID TASK_GROUP
1123947 8/3/20 13:35 8/3/20 13:36 0       1
2343946 8/3/20 13:35 8/3/20 13:38 2       3
5123945 8/3/20 13:32 8/3/20 13:34 2       1
3982344 8/3/20 13:32 8/3/20 13:35 4       2
3921343 8/3/20 13:30 8/3/20 13:32 4       1
3981232 8/3/20 13:29 8/3/20 13:30 4       6
3985423 8/3/20 13:27 8/3/20 13:35 7       1
3983432 8/3/20 13:26 8/3/20 13:35 0       1
3983234 8/3/20 13:26 8/3/20 13:35 3       4
3981230 8/3/20 13:23 8/3/20 13:35 6       1
3983407 8/3/20 13:21 8/3/20 13:29 4       4
3983936 8/3/20 13:20 8/3/20 13:32 2       1
3983213 8/3/20 13:20 8/3/20 13:27 7       3
3921432 8/3/20 13:19 8/3/20 13:20 2       1
3983567 8/3/20 13:19 8/3/20 13:26 0       5

其中每个任务的开始时间和停止时间,任务所属的组,对应的人都给出了。

如何在 ggplot2 中创建甘特图?似乎没有这样做的参数。

【问题讨论】:

  • 您的第二行持续时间为负数...错字?
  • 如果您没有绑定到ggplot2,您可能需要查看DiagrammR,它还可以生成交互式甘特图,请参阅this SO answertimevis
  • @Wimpel 是的,这是一个错字,抱歉!

标签: r ggplot2 dplyr data-visualization


【解决方案1】:

如果您不受ggplot2 约束并且喜欢交互式使用:我推荐timevis 库。

在代码中使用您的数据如下所示:

library(timevis)
library(tidyverse)

df <- tribble(
  ~ASK_ID, ~START_TIME_date, ~START_TIME_hour, ~STOP_TIME_date, ~STOP_TIME_hour, ~PERSON_ID, ~TASK_GROUP,
  3983947, "8/3/20", "13:35", "8/3/20", "13:36", 100, 1,
  3983946, "8/3/20", "13:35", "8/3/20", "13:38", 102, 3,
  3983945, "8/3/20", "13:32", "8/3/20", "13:34", 102, 1,
  3983944, "8/3/20", "13:32", "8/3/20", "13:35", 104, 2,
  3983943, "8/3/20", "13:30", "8/3/20", "13:32", 104, 1,
  3983942, "8/3/20", "13:29", "8/3/20", "13:30", 104, 6,
  3983941, "8/3/20", "13:27", "8/3/20", "13:35", 107, 1,
  3983940, "8/3/20", "13:26", "8/3/20", "13:35", 100, 1,
  3983939, "8/3/20", "13:26", "8/3/20", "13:35", 103, 4,
  3983938, "8/3/20", "13:23", "8/3/20", "13:35", 106, 1,
  3983937, "8/3/20", "13:21", "8/3/20", "13:29", 104, 4,
  3983936, "8/3/20", "13:20", "8/3/20", "13:32", 102, 1,
  3983935, "8/3/20", "13:20", "8/3/20", "13:27", 107, 3,
  3983934, "8/3/20", "13:19", "8/3/20", "13:20", 102, 1,
  3983933, "8/3/20", "13:19", "8/3/20", "13:26", 100, 5
) %>% 
  mutate(start_time = as.POSIXct(paste(START_TIME_date, START_TIME_hour)),
         stop_time = as.POSIXct(paste(STOP_TIME_date, STOP_TIME_hour)))

time_data <- df %>% 
  transmute(
    id = 1:n(),
    content = paste("Task", ASK_ID),
    start = start_time,
    end = stop_time,
    group = PERSON_ID
  )
time_data
#> # A tibble: 15 x 5
#>       id content      start               end                 group
#>    <int> <chr>        <dttm>              <dttm>              <dbl>
#>  1     1 Task 3983947 8-03-20 13:35:00    8-03-20 13:36:00      100
#>  2     2 Task 3983946 8-03-20 13:35:00    8-03-20 13:38:00      102
#>  3     3 Task 3983945 8-03-20 13:32:00    8-03-20 13:34:00      102
#>  4     4 Task 3983944 8-03-20 13:32:00    8-03-20 13:35:00      104
#>  5     5 Task 3983943 8-03-20 13:30:00    8-03-20 13:32:00      104
#>  6     6 Task 3983942 8-03-20 13:29:00    8-03-20 13:30:00      104
#>  7     7 Task 3983941 8-03-20 13:27:00    8-03-20 13:35:00      107
#>  8     8 Task 3983940 8-03-20 13:26:00    8-03-20 13:35:00      100
#>  9     9 Task 3983939 8-03-20 13:26:00    8-03-20 13:35:00      103
#> 10    10 Task 3983938 8-03-20 13:23:00    8-03-20 13:35:00      106
#> 11    11 Task 3983937 8-03-20 13:21:00    8-03-20 13:29:00      104
#> 12    12 Task 3983936 8-03-20 13:20:00    8-03-20 13:32:00      102
#> 13    13 Task 3983935 8-03-20 13:20:00    8-03-20 13:27:00      107
#> 14    14 Task 3983934 8-03-20 13:19:00    8-03-20 13:20:00      102
#> 15    15 Task 3983933 8-03-20 13:19:00    8-03-20 13:26:00      100

group_data <- time_data %>% 
  distinct(group) %>% 
  mutate(id = group, content = paste("Person", group))
group_data
#> # A tibble: 6 x 3
#>   group    id content   
#>   <dbl> <dbl> <chr>     
#> 1   100   100 Person 100
#> 2   102   102 Person 102
#> 3   104   104 Person 104
#> 4   107   107 Person 107
#> 5   103   103 Person 103
#> 6   106   106 Person 106

reprex package (v0.3.0) 于 2020 年 8 月 31 日创建

timevis(time_data, groups = group_data)

【讨论】:

    【解决方案2】:

    这是我的尝试..

    library( data.table )
    library( ggplot2 )
    library( gridExtra )   #for merging two plots together
    
    #sample data 
    # !! ALTERED STOP_TIME IN ROW2 !!
    DT <- fread("TASK_ID START_TIME   STOP_TIME     PERSON_ID TASK_GROUP
    3983947 8/3/20T13:35 8/3/20T13:36 100       1
    3983946 8/3/20T13:35 8/3/20T13:37 102       3
    3983945 8/3/20T13:32 8/3/20T13:34 102       1
    3983944 8/3/20T13:32 8/3/20T13:35 104       2
    3983943 8/3/20T13:30 8/3/20T13:32 104       1
    3983942 8/3/20T13:29 8/3/20T13:30 104       6
    3983941 8/3/20T13:27 8/3/20T13:35 107       1
    3983940 8/3/20T13:26 8/3/20T13:35 100       1
    3983939 8/3/20T13:26 8/3/20T13:35 103       4
    3983938 8/3/20T13:23 8/3/20T13:35 106       1
    3983937 8/3/20T13:21 8/3/20T13:29 104       4
    3983936 8/3/20T13:20 8/3/20T13:32 102       1
    3983935 8/3/20T13:20 8/3/20T13:27 107       3
    3983934 8/3/20T13:19 8/3/20T13:20 102       1
    3983933 8/3/20T13:19 8/3/20T13:26 100       5")
    
    #set timestamps to posixct
    cols = grep( "TIME$", names(DT), value = TRUE )
    DT[, (cols) := lapply( .SD, as.POSIXct, format = "%d/%m/%yT%H:%M"), .SDcols = cols ]
    
    #create barchart
    plot1 <- ggplot( data = DT ) + 
      geom_rect( aes( xmin = START_TIME, 
                      xmax = STOP_TIME, 
                      ymin = 0, 
                      ymax = 1, 
                      fill = as.factor(TASK_GROUP) ) ) +
      facet_wrap( ~PERSON_ID, ncol = 1, scales = "free_x" ) +
      coord_cartesian( xlim = c( min( DT$START_TIME, na.rm = TRUE ), max( DT$STOP_TIME, na.rm = TRUE ) ) ) +
      theme(axis.title.y = element_blank(),
            axis.text.y  = element_blank(),
            axis.ticks.y = element_blank()) + 
      guides(fill = FALSE)
    
    #prepare data for piecharts
    DT.pie <- DT[, duration := as.numeric( STOP_TIME - START_TIME ) ]
    #calculate percentages
    DT.pie[, percentage := duration / sum( duration ), by = .(PERSON_ID) ]
    
    #create piechart
    plot2 <- ggplot( data = DT.pie, aes( x = 1, y = percentage, fill = as.factor( TASK_GROUP ) ) ) +
      geom_bar( width = 1, stat = "identity" ) +
      facet_wrap( ~PERSON_ID, ncol = 1 ) +
      coord_polar(theta = "y", start=0) +
      theme(axis.title.x = element_blank(),
            axis.text.x  = element_blank(),
            axis.ticks.x = element_blank(),
            axis.title.y = element_blank(),
            axis.text.y  = element_blank(),
            axis.ticks.y = element_blank(),) + 
      labs( fill = "Task" )
    
    #combine barchart and piechart
    grid.arrange(plot1, plot2, ncol=2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多