Je travaille actuellement sur un projet avec les données fournies:

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

Où l'heure de début et l'heure de fin de chaque tâche, le groupe auquel appartient la tâche et la personne à laquelle elle correspond sont indiqués.

Comment créer un diagramme de Gantt dans ggplot2? Il semble qu'il n'y ait pas de paramètre pour le faire.

-1
Z Oscar 31 août 2020 à 10:34

2 réponses

Meilleure réponse

Voici mon essai ...

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)

enter image description here

4
Wimpel 31 août 2020 à 08:30

Si vous n'êtes pas lié à ggplot2 et que vous avez envie d'une utilisation interactive: je recommande le timevis < / a> bibliothèque.

L'utilisation de vos données avec le code ressemble à ceci:

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

Créé le 2020-08-31 par le package reprex (v0.3.0)

timevis(time_data, groups = group_data)

0
David 31 août 2020 à 13:02