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')

  

posted @ 2017-10-30 14:29  小肥羊的博客  阅读(1590)  评论(0编辑  收藏  举报