今天,在群里看见有人问R能不能绘制甘特图,觉得很有意思 ,就动手试了一下,虽说很丑,但也说明R几乎无所不能。
代码如下:
GanttChart <- function(program,startdate,enddate,title = NA) { startdate <- as.Date(startdate) enddate <- as.Date(enddate) if(sum(startdate > enddate) > 0) { stop(\'error:start date is later than end date.\') } progrm <- length(program):1 plot(NA,NA, xlim = c(min(startdate),max(enddate)), ylim = range(progrm), xlab = \'日期\', ylab = \'项目\', xaxt = \'n\', yaxt = \'n\', main = title) startUnionEnd <- as.Date(origin = \'1970-01-01\',union(startdate,enddate)) axis(1,startUnionEnd,startUnionEnd) axis(2,progrm,program) segments(startdate,progrm,enddate,progrm,lwd = 5) abline(v = startUnionEnd) } program <- c(\'P1\',\'P2\',\'P3\',\'P4\') startdate <- seq(from = as.Date(\'1994-09-02\'),by = 5,length.out = 4) enddate <- seq(from = as.Date(\'1994-10-21\'),by = 5,length.out = 4) GanttChart(program,startdate,enddate,\'GANTT CHART\')
请发表评论