下面的代码生成一个甘特图,按人对每个项目的时间。它分两层完成,黑条代表一个人的 100% 时间,彩色条代表项目的时间。
我想要的是像图像一样堆叠的项目栏,所以很明显 Jane 在 12 月是 100% 承诺的。我不在乎它们堆叠的顺序。
是否有可能或者我应该瞄准每人 1 个地块,然后是 multiplot()?
library(ggplot2)
#create some data
gantt <- data.frame(name=c("Jane","John","Jane","John","Jane","Joe"),
start=c("20/10/21","15/8/21","11/11/21",
"17/11/21","23/9/21","28/10/21"),
end=c("20/12/21","15/11/21","11/1/22",
"11/12/21","23/12/21","28/1/22"),
percent=c(0.5,0.8,0.35,0.7,0.15,0.7),
Project=c("RES/1/1","RES/1/1","RES/1/2",
"RES/1/2","RES/1/3","RES/1/3"))
#format the dates
gantt$startD <- as.Date(gantt$start,format="%d/%m/%y")
gantt$endD <- as.Date(gantt$end,format="%d/%m/%y")
#make a 100% data set from start to end representing 'all' time
allTime <- data.frame(name=unique(gantt$name),
startD=min(gantt$startD, na.rm = T),
endD=max(gantt$endD, na.rm = T))
#plot
ggplot(allTime, aes(x=startD, xend=endD, y=name, yend=name)) +
theme_bw()+ #use ggplot theme with black gridlines and white background
#plot the 'all' time bars
geom_segment(size=30) + #increase line width of segments in the chart
#plot the Project time bars
geom_segment(data=gantt,
aes(x=startD, xend=endD, y=name, yend=name, col=Project),
position = 'identity',
size=30*gantt$percent) + #width of segments * percent of time
#label
labs(title='Project Gantt Chart', x='Date', y='Name')