Gantt Charts in R
How to make a gantt charts in R with Plotly
New to Plotly?
Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.
Gantt Chart
library(plotly)
# Read in data
df <- read.csv("https://cdn.rawgit.com/plotly/datasets/master/GanttChart-updated.csv", stringsAsFactors = F)
# Convert to dates
df$Start <- as.Date(df$Start, format = "%m/%d/%Y")
# Sample client name
client = "Sample Client"
# Choose colors based on number of resources
cols <- RColorBrewer::brewer.pal(length(unique(df$Resource)), name = "Set3")
df$color <- factor(df$Resource, labels = cols)
# Initialize empty plot
fig <- plot_ly()
# Each task is a separate trace
# Each trace is essentially a thick line plot
# x-axis ticks are dates and handled automatically
for(i in 1:(nrow(df) - 1)){
fig <- add_trace(fig,
x = c(df$Start[i], df$Start[i] + df$Duration[i]), # x0, x1
y = c(i, i), # y0, y1
mode = "lines",
line = list(color = df$color[i], width = 20),
showlegend = F,
hoverinfo = "text",
# Create custom hover text
text = paste("Task: ", df$Task[i], "<br>",
"Duration: ", df$Duration[i], "days<br>",
"Resource: ", df$Resource[i]),
evaluate = T # needed to avoid lazy loading
)
}
fig
Alter Layout
# Add information to plot and make the chart more presentable
fig <- layout(fig,
# Axis options:
# 1. Remove gridlines
# 2. Customize y-axis tick labels and show task names instead of numbers
xaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6")),
yaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6"),
tickmode = "array", tickvals = 1:nrow(df), ticktext = unique(df$Task),
domain = c(0, 0.9)),
plot_bgcolor = "#333333", # Chart area color
paper_bgcolor = "#333333") # Axis area color
fig
Add Annotations
# Add total duration and total resources used
# x and y coordinates are based on a domain of [0,1] and not
# actual x-axis and y-axis values
a <- list(xref = "paper",
yref = "paper",
x = 0.80,
y = 0.1,
text = paste0("Total Duration: ", sum(df$Duration), " days<br>",
"Total Resources: ", length(unique(df$Resource)), "<br>"),
font = list(color = '#264E86', size = 12),
ax = 0,
ay = 0,
align = "left",
showarrow = FALSE)
# Add client name and title on top
b <- list(xref = "paper",
yref = "paper",
x = 0.1,
y = 1,
xanchor = "left",
text = paste0("Gantt Chart: ", client),
font = list(color = '#264E86', size = 20, family = "Times New Roman"),
ax = 0,
ay = 0,
align = "left",
showarrow = FALSE)
fig <- fig %>% layout(annotations = a)
fig <- fig %>% layout(annotations = b)
fig
for more information please refer to http://moderndata.plot.ly/gantt-charts-in-r-using-plotly/
What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig
, you can display the same figure in a Dash for R application by passing it to the figure
argument of the Graph
component from the built-in dashCoreComponents
package like this:
library(plotly)
fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
)
app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)