[R] Code contributed for a gant (Gantt) chart

Waichler, Scott R Scott.Waichler at pnl.gov
Fri Jan 14 17:25:10 CET 2005


I searched CRAN for material on gant (or Gantt) charts, those schedule
plots that look like horizontal barplots where the x-axis is time.
Finding none, I wrote the following and am submitting it to the archive
so that it may help someone in the future.

plotfile <- "gant.eps"

regular.color <- "lightgray"
important.color <- "yellow"

tasks <- list()  # Define tasks.  Start and end refer to times.
tasks[[length(tasks) + 1]] <-
  list(name = "Name of first task", start = 0, end = 3, color =
regular.color)
tasks[[length(tasks) + 1]] <-
  list(name = "Name of second task", start = 3, end = 7, color =
regular.color)
tasks[[length(tasks) + 1]] <-
  list(name = "Name of third task", start = 3, end = 7, color =
regular.color)
tasks[[length(tasks) + 1]] <-
  list(name = "Name of fourth task", start = 5, end = 9, color =
important.color)
tasks[[length(tasks) + 1]] <-
  list(name = "Name of fifth task", start = 3, end = 8, color =
regular.color)
tasks[[length(tasks) + 1]] <-
  list(name = "Name of sixth task", start = 3, end = 11, color =
important.color)

num.tasks <- length(tasks)
task.labels <- rep(NA, num.tasks)
task.times <- NULL
for(i in 1:num.tasks) {
  task.labels[i] <- paste(i, ". ", tasks[[i]]$name)
  task.times <- c(task.times, tasks[[i]]$start, tasks[[i]]$end)
}

# Make the plot
ps.options(onefile=T, print.it=F, paper="special", width=8.5, height=4,
          horizontal=F, pointsize=12, family="Helvetica")
postscript(file=plotfile)

par(omi=c(0.05, 2, 0.5, 0.1))
par(mar=c(0, 2, 0.60, 0.05))

plot(1:3, 1:3,
     ylim=c(1, num.tasks) , xlim=range(task.times),
     main="", xlab = "",
     xaxs="i", xaxt="n", yaxt="n", ylab="", 
     type="n")
par(mgp = c(3, 0.5, 0))
axis(3, at=min(task.times):max(task.times),
labels=min(task.times):max(task.times),
     cex.axis=1.0, las=1, tcl=-0.3)
reverse.task.numbers <- rev(1:num.tasks)
axis(2, at=reverse.task.numbers, labels=task.labels, cex.axis=1.0,
las=1, tcl=-0.2)
abline(v=c(min(task.times):max(task.times)), col="darkgray", lty=3)

half.height <- 0.25
for(i in 1:num.tasks) {
  rect(tasks[[i]]$start, reverse.task.numbers[i] - half.height,
tasks[[i]]$end,
       reverse.task.numbers[i] + half.height, col = tasks[[i]]$color,
border = F)
}
box()

mtext("Month", side=3, outer=F, line=2, cex=1.1) # x-axis label

dev.off()

Scott

Scott Waichler, Senior Research Scientist
Pacific Northwest National Laboratory
MSIN K9-36
P.O. Box 999
Richland, WA   99352    USA
509-372-4423 (voice)
509-372-6089 (fax)
scott.waichler at pnl.gov
http://hydrology.pnl.gov




More information about the R-help mailing list