【发布时间】:2020-10-15 01:57:23
【问题描述】:
我从另一个帖子中借用了示例数据,并根据我的情况进行了修改。 (Add shading to a gantt chart to delineate weekends)
我绘制甘特图的问题是我的数据在该类别中包含多个时期。 在下面的示例中,我为“撰写介绍”添加了两个句点,并为“撰写结果”添加了一个句点。
另外,我想为符合条件的特定时段着色。在这里,如果某个时期的任何部分落在 5 月或 8 月,我将其标记出来。
似乎在一个类别中最多有两个时期可以正常工作。 但是当有三个时期时,它们会合并为一个时期?!
我的真实数据集要复杂得多,一个类别有时有超过 10 个周期,并且有非常明确的标记标准。
我不确定如何解决这个问题。
require(reshape2)
require(ggplot2)
# Create a list of tasks name strings.
tasks <- c("Write introduction", "Parse citation data",
"Construct data timeline",
"Write methods", "Model formulation",
"Model selection", "Write results", "Write discussion",
"Write abstract and editing",
"Write introduction", "Write introduction", "Write results")
# Compile dataframe of task names, and respective start and end dates.
dfr <- data.frame(
name = factor(tasks, levels = tasks[1:9]),
start.date = as.Date(c("2018-04-09", "2018-04-09", "2018-04-16",
"2018-04-30", "2018-04-16", "2018-05-21",
"2018-06-04", "2018-07-02", "2018-07-30",
"2018-05-15", "2018-06-03", "2018-07-25"
)),
end.date = as.Date(c("2018-04-30", "2018-04-20", "2018-05-18",
"2018-06-01", "2018-05-18", "2018-06-01",
"2018-06-29", "2018-07-27", "2018-08-31",
"2018-05-29", "2018-06-20", "2018-08-15")),
flag = c(0, 0, 1,
1, 1, 1,
0, 0, 1,
1, 0, 1)
)
# Merge start and end dates into durations.
mdfr <- melt(dfr, measure.vars = c("start.date", "end.date"))
# gannt chart
ggplot(mdfr) +
geom_line(aes(value, name, colour = as.factor(flag)), size = 4) +
labs(title = "Project gantt chart",
x = NULL,
y = NULL) +
theme_minimal()
【问题讨论】: