利用甘特图(Gantt chart)管理学习计划,通过条形来显示项目的进度、时间安排等相关情况。
一 数据准备
加载函数包,读入数据
代码语言:javascript复制library(plotly)
# 读取数据
df <- read.csv("GanttChart-July.csv", stringsAsFactors = F)
# 转为Date类型
df$Start <- as.Date(df$Start, format = "%m/%d/%Y")
head(df)
绘制甘特图
数据如上,只需任务内容Task,开始时间Start,持续的时间数据即可绘制甘特图。
代码语言:javascript复制# 根据Resource 设置甘特图的颜色
cols <- RColorBrewer::brewer.pal(length(unique(df$Resource)), name = "Set3")
df$color <- factor(df$Resource, labels = cols)
# 初始一个空对象
p <- plot_ly()
# 每个task一个line plot,起始时间和任务持续时长
for(i in 1:(nrow(df) - 1)){
p <- add_trace(p,
x = c(df$Start[i], df$Start[i] df$Duration[i]), #起试和终止
y = c(i, i), #
mode = "lines",
line = list(color = df$color[i], width = 20), #设置line plot的颜色和线宽
showlegend = F,
hoverinfo = "text", #悬浮信息为text信息
#设置text显示内容
text = paste("Task: ", df$Task[i], "<br>",
"Duration: ", df$Duration[i], "days<br>",
"Resource: ", df$Resource[i]),
evaluate = T ,
xaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6")),
yaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6"),
tickmode = "array", tickvals = 1:nrow(df), ticktext = unique(df$Task),
domain = c(0, 0.9)),
plot_bgcolor = "#333333", # 设置颜色
paper_bgcolor = "#333333"
)
}
更改Layout信息
- 去掉 gridlines
- 显示task任务
- 更改背景颜色
代码语言:javascript复制p <- layout(p,
xaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6")),
yaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6"),
tickmode = "array", tickvals = 1:nrow(df), ticktext = unique(df$Task),
domain = c(0, 0.9)),
plot_bgcolor = "#333333", # Chart area color
paper_bgcolor = "#333333") # Axis area color