Gantt chart using Gvis

Gantt chart can be very useful to visualize the occurance of certain events on a timeline. If you need create one programmetically, you can use gvis package which utilizes plotting library from Google.

# get the data
df <- data_get() # make sure you get the data here

# convert time to decimal hours, remove if not required
df$start.ct   <- as.POSIXct(paste(df$`Date Time`, df$start, sep = " "))
df$end.ct     <- as.POSIXct(paste(df$`Date Time`, df$end, sep = " "))
df$start.hour <- as.POSIXlt(df$start.ct)$hour + 
                    as.POSIXlt(df$start.ct)$min/60 + 
                    as.POSIXlt(df$start.ct)$sec/3600
df$end.hour   <- as.POSIXlt(df$end.ct)$hour + 
                    as.POSIXlt(df$end.ct)$min/60 + 
                    as.POSIXlt(df$end.ct)$sec/3600

# do this if your start time and end time is same in the df
# add one hour to start time if this is hourly, remove if needed
df$endTime <- df$start.ct+3600              

library(googleVis)
datTL <- data.frame(Position='',            # bar labels
                    Name=df$`Child Object`, # row labels
                    start=df$start.ct,      # the start time
                    end=df$endTime)         # the end time

# the actual Gantt chart. If using in shiny, render via renderGvis
Timeline <- 
  gvisTimeline(
    data=datTL, 
    rowlabel="Name",
    barlabel="Position",
    start="start", 
    end="end",
    options=list(timeline="{groupByRowLabel:true}",  # one row per label
                width='100%',                        
                height=700,                          
                backgroundColor='#ffd', 
                height=350,
                colors="['#cbb69d', '#603913', '#c69c6e']"))  # colors to be used

# testing the code
# It should be a gvis class
cat('class Timeline:', class(Timeline),'\n')         
comments powered by Disqus