1

The code below produces a gantt chart, time on each project by person. It's done in two layers where the black bars are 100% of a persons time and the coloured bars represent time on projects.

What I'd like is the project bars stacked like the image so it's obvious Jane is 100% committed in Dec. I don't care what order they're stacked in.

Is it possible or should I be aiming for 1 plot per person and then 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')

desired_plot

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
JohnC
  • 15
  • 3

0 Answers0