Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R可视化-创建甘特图_R_Ggplot2_Dplyr_Data Visualization - Fatal编程技术网

R可视化-创建甘特图

R可视化-创建甘特图,r,ggplot2,dplyr,data-visualization,R,Ggplot2,Dplyr,Data Visualization,我目前正在使用提供的数据进行一个项目: 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

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

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中创建甘特图?似乎没有一个参数可以这样做。

我来试试

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)
库(data.table)
图书馆(GG2)
库(gridExtra)#用于将两个绘图合并在一起
#样本数据
# !! 更改了第2行中的停止时间!!

DT如果您没有绑定到
ggplot2
并喜欢交互式使用:我建议使用该库

将数据与代码一起使用如下所示:

库(timevis)
图书馆(tidyverse)
df%
mutate(start_time=as.POSIXct(粘贴(start_time_date,start_time_hour)),
stop\u time=as.POSIXct(粘贴(stop\u time\u date,stop\u time\u hour)))
时间单位数据%
蜕变(
id=1:n(),
内容=粘贴(“任务”,询问ID),
开始=开始时间,
结束=停止时间,
组=个人ID
)
时间单位数据
#>#A tibble:15 x 5
#>id内容开始结束组
#>                                         
#>1任务3983947 8-03-20 13:35:00 8-03-20 13:36:00 100
#>2任务3983946 8-03-20 13:35:00 8-03-20 13:38:00 102
#>3任务3983945 8-03-20 13:32:00 8-03-20 13:34:00 102
#>4任务3983944 8-03-20 13:32:00 8-03-20 13:35:00 104
#>5任务3983943 8-03-20 13:30:00 8-03-20 13:32:00 104
#>6任务3983942 8-03-20 13:29:00 8-03-20 13:30:00 104
#>7任务3983941 8-03-20 13:27:00 8-03-20 13:35:00 107
#>8任务3983940 8-03-20 13:26:00 8-03-20 13:35:00 100
#>9任务3983939 8-03-20 13:26:00 8-03-20 13:35:00 103
#>10任务3983938-03-20 13:23:00 8-03-20 13:35:00 106
#>11任务3983937 8-03-20 13:21:00 8-03-20 13:29:00 104
#>12任务3983936 8-03-20 13:20:00 8-03-20 13:32:00 102
#>13任务3983935 8-03-20 13:20:00 8-03-20 13:27:00 107
#>14任务3983934 8-03-20 13:19:00 8-03-20 13:20:00 102
#>15任务3983933 8-03-20 13:19:00 8-03-20 13:26:00 100
组数据%
不同(组)%>%
变异(id=组,内容=粘贴(“人”,组))
分组数据
#>#tibble:6 x 3
#>组id内容
#>          
#>1100人100人
#>2 102人102
#>3 104人104
#>40107人107
#>5 103人103
#>6 106人106
由(v0.3.0)于2020年8月31日创建

timevis(时间数据,组=组数据)

第二行的持续时间为负。。。输入错误?如果您没有绑定到
ggplot2
,您可能希望签出,这也会生成交互式甘特图,请参见或@Wimpel yes这是一个输入错误,抱歉!