甘特图:在特定日期添加行时出现的问题

时间:2015-12-23 11:50:52

标签: r gantt-chart

我正在尝试准备甘特图,我希望在特定日期包括几行。我的问题是根据指定的甘特网格位置没有正确放置这些线。

这是我的代码:

library(plotrix)
Ymd.format<-"%Y/%m/%d"
gantt.info<-list(
labels = c("Task A", "Task B"),
starts = as.POSIXct(strptime(c("2011/12/01", "2012/01/15"), format=Ymd.format, tz="GMT")),
ends = as.POSIXct(strptime(c("2012/01/15", "2012/12/01"), format=Ymd.format, tz="GMT")))

vgridpos<- as.POSIXct(strptime(c("2011/12/01","2012/01/01","2012/02/01","2012/03/01",
  "2012/04/01","2012/05/01","2012/06/01","2012/07/01","2012/08/01",
  "2012/09/01","2012/10/01","2012/11/01"), format=Ymd.format, tz="GMT"))
vgridlab<- c("Dec-2011","Jan-2012","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov")

x11()
gantt.chart(gantt.info, priority.legend=F, vgridpos=vgridpos, vgridlab=vgridlab, hgrid=TRUE, label.cex = 1,
            taskcolors=c(1,2))

# Add reference lines
abline(v = ISOdate(2011,12,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,1,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,2,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,3,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,4,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,5,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,6,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,7,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,8,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,9,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,10,1,tz="GMT"), lty=4, col = 3)
abline(v = ISOdate(2012,11,1,tz="GMT"), lty=4, col = 3)

在此示例中,灰色垂直线应与绿线重合......但它们不会......

任何人都可以帮助我吗?

非常感谢,

1 个答案:

答案 0 :(得分:0)

您必须将绘图边距设置为与gantt-chart使用的绘图边距相同。

尝试以下方法:

# Set up plot region to be the same as gantt.chart
priority.legend = FALSE
xlab = ""
charheight <- strheight("M", units = "inches")
maxwidth <- max(strwidth(gantt.info$labels, units = "inches")) + 0.3
bottom.margin <- ifelse(priority.legend || nchar(xlab), 0.7, 0)
par(mai = c(bottom.margin, maxwidth, charheight * 5, 0.1))
par(omi = c(0.1, 0.1, 0.1, 0.1), xaxs = "i", yaxs = "i")

# Add reference lines
dates = c("2011-12-01 00:00:00 GMT",
          "2012-01-01 00:00:00 GMT",
          "2012-02-01 00:00:00 GMT",
          "2012-03-01 00:00:00 GMT",
          "2012-04-01 00:00:00 GMT",
          "2012-05-01 00:00:00 GMT",
          "2012-06-01 00:00:00 GMT",
          "2012-07-01 00:00:00 GMT",
          "2012-08-01 00:00:00 GMT",
          "2012-09-01 00:00:00 GMT",
          "2012-10-01 00:00:00 GMT",
          "2012-11-01 00:00:00 GMT")

posix.dates = as.POSIXct(dates, tz = "GMT")

lapply(posix.dates,
      function(x) {
        abline(v = x, lty=4, col = 6)
      })