Skip to content

Instantly share code, notes, and snippets.

@chr1swallace
Created November 7, 2014 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chr1swallace/68e0ff7076213a80b17c to your computer and use it in GitHub Desktop.
Save chr1swallace/68e0ff7076213a80b17c to your computer and use it in GitHub Desktop.
create a timeline
# replace "gantt.csv" with path to wherever your .csv file is stored
x <- read.table("gantt.csv",sep=",",as.is=TRUE, header=TRUE) # read data
## look at what we read, check it makes sense
head(x)
## make sure dates are recognised as such
x$Start <- as.Date(x$Start)
x$End <- as.Date(x$End)
## midpoint in each date range, for positioning text labels
x$Mid <- x$Start + (x$End - x$Start)/2
## check it makes sense
head(x)
## THIS BIT IS COMPLICATED! IT'S ABOUT ORDERING OF ROWS. YOU COULD DO
## THE SAME MY MANUALLY ORDERING ROWS IN YOUR EXCEL TABLE, OR ASK ME
## TO UPDATE THIS BIT IF THINGS CHANGE, BUT IT'S A HACKY USE OF R FOR
## DATA MANIPULATION WHERE PYTHON OR SUCH MIGHT DO BETTER. FOCUS ON
## UNDERSTANDING OTHER BITS OF R, BUT NOT THIS, JUST YET
## order studies by start date
sd <- subset(x,Class=="Collection")
sd <- sd[order(sd$Start),]
sd <- rbind(subset(x,Name=="DILT1D" & Class=="Clinical analysis"),
sd)
x$Name <- factor(x$Name,levels=sd$Name)
## then by class
x$Class <- factor(x$Class,levels=c("Planning","Collection","Clinical analysis","Mech analysis"))
x <- x[order(x$Name,x$Class),]
## row is complicated. Want all Planning Collection Clin on same row, and before Mech
x <- rbind(subset(x,Class!="Mech analysis"), subset(x,Class=="Mech analysis"))
d <- pmax(c(1,diff(x$Name)),0)
x$row <- cumsum(d)
## check it makes sense
head(x)
tail(x) # mech should be at bottom
## arrows from start of anything with Class Collection to start of Mech with same name
m <- merge(x=subset(x,Class=="Collection",select=c("Name","Start")),
y=subset(x,Class=="Mech analysis",select=c("Name","Start")),
by="Name",
suffixes=c("Collection","Mech"))
## check it makes sense
m
library(grid) # functions to draw arrows
library(ggplot2) # functions to make plotting easy
## make plot
p <- ggplot(x) + # x holds our data
geom_rect(aes(xmin=Start,xmax=End,ymin=row-0.4,ymax=row+0.4,
fill=Class), # draw filled rectangles and create a legend
alpha=0.5) + # make the filling a bit transparent
geom_text(aes(x=Mid,y=row,label=Text),size=4) + # add text
geom_segment(mapping=aes(x=StartCollection,xend=StartMech,y=rowCollection,yend=rowMech),
arrow = arrow(length = unit(0.1,"cm")),
linetype="dashed",
data=m) + # add arrows - the data for this is in m
labs(x="Year",y="Study") + # axis labels
scale_y_reverse(breaks=unique(x$row),labels=x$Name[!duplicated(x$row)]) # draw the y axis upside down and label each row
## draw to current graphics device, which is screen by default
p
## send to tiff
tiff("gantt.tiff",height=700,width=1000) # open stream to graphics device
p # draw the plot on open device
dev.off() # close that device
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment