Skip to content

Instantly share code, notes, and snippets.

@tomravalde
Last active June 20, 2017 18:14
Show Gist options
  • Save tomravalde/d81ac74838ee1ec54e95 to your computer and use it in GitHub Desktop.
Save tomravalde/d81ac74838ee1ec54e95 to your computer and use it in GitHub Desktop.
Gantt chart (ggplot)
# Gantt chart based on my PhD completion timetable.
## Below is a copy of gantt.csv (built initially in a spreadsheet)
#Task,Start,End,Chapter
#"Metabolic efficiency reading (3)",2014/08/01,2014/11/30,3
#"Metabolic efficiency data analysis (3)",2014/11/01,2015/02/28,3
#"Model development (4)",2014/08/01,2014/10/30,4
#"Shann Gu case study (5)",2014/08/01,2014/10/30,5
#"Metabolic pathway database construction (6)",2014/08/01,2015/04/30,6
#"Metabolic pathaway analysis (6)",2015/03/01,2015/06/30,6
#"Consolidate discussion and conclusions (7)",2015/07/01,2015/08/31,7
#"Write up (all)",2015/09/01,2016/02/29,all
library(ggplot2)
library(scales)
gantt <- read.csv("gantt.csv")
## Convert dates to date class (so scale_x_date can be applied)
gantt$Start <- as.Date(gantt$Start, format='%Y/%m/%d')
gantt$End <- as.Date(gantt$End, format='%Y/%m/%d')
fig.gantt <- ggplot(gantt) +
geom_segment(aes(x=Start, xend=End, y=Task, yend=Task), size=3) +
theme_bw() +
## set order of the y-axis with 'limits'
scale_y_discrete(limits=c("Write up (all)",
"Consolidate discussion and conclusions (7)",
"Metabolic pathaway analysis (6)",
"Metabolic pathway database construction (6)",
"Metabolic efficiency data analysis (3)",
"Metabolic efficiency reading (3)",
"Shann Gu case study (5)",
"Model development (4)")) +
## labels dates as abbreviated months, i.e. (Jan Feb Mar ...)
scale_x_date(breaks = "1 month", labels=date_format("%b")) +
theme(legend.position="top",
text=element_text(family="Linux Biolinum", size=10),
axis.title.x=element_blank(), axis.title.y=element_blank()) +
scale_color_grey()
ggsave("gantt-chart.pdf", fig.gantt, width=9, height=4, dpi=700)
embed_fonts("gantt-chart.pdf")
# Additionly you could map a color aesthetic to attributes such as chapter contribution, activity type etc.
# E.g. fig.results <- ggplot(gantt, aes(color=Chapter)) + ...
@cristianvaldez
Copy link

I can't see your gant csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment