原来甘特图(Gantt Chart)还可以这么美!赶快掌握下绘制方法吧~~

2022-02-17 14:55:37 浏览数 (1)

今天这篇推文小编给大家介绍下甘特图(Gantt Chart) 及其绘制方法,主要内容如下:

  • 甘特图(Gantt Chart) 的简单介绍
  • 甘特图(Gantt Chart) 绘制方法(R Python)

甘特图(Gantt Chart) 介绍

甘特图(Gantt chart) 又称为横道图、条状图(Bar chart)。其通过条状图来显示项目、进度和其他时间相关的系统进展的内在关系随着时间进展的情况。一般情况下,横轴表示时间,纵轴表示各个项目,线条表示期间计划和实际完成的情况。样例如下:

甘特图(Gantt Chart)样例

那么接下来,小编就告诉大家如何使用Python和R绘制甘特图(Gantt Chart)。

甘特图(Gantt Chart) 绘制方法(R Python)

这一部分,小编分别使用Python和R绘制甘特图(Gantt Chart),小伙伴们可根据自己喜好选择合适的工具进行绘制哈~

甘特图(Gantt Chart)Python绘制

我们还是使用Python-matplotlib包进行绘制,这里使用到的绘图函数为broken_barh() 函数,该函数用于绘制一系列水平的矩形,正好可以满足甘特图的绘制需求。样例如下:

代码语言:javascript复制
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = "Times New Roman"
fig, ax = plt.subplots(figsize=(6.5,4),dpi=100,facecolor="white")

ax.broken_barh([(20, 30),(70,30)], (10, 5), facecolors ='#BC3C28',edgecolor="black",label="Project_1",zorder=2)
ax.broken_barh([(50, 40), (100, 40)], (20, 5),
                facecolors ='#0972B5',edgecolor="black",label="Project_2",zorder=2)
ax.broken_barh([(10, 50), (100, 20), (130, 10)], (30, 5),
                facecolors =('#E28726'),edgecolor="black",label="Project_3",zorder=2)
ax.broken_barh([(5, 30), (40, 40), (100, 40)], (40, 5),
                facecolors =('#21854D'),edgecolor="black",label="Project_4",zorder=2)
ax.set_ylim(5, 50)
ax.set_xlim(-5, 165)
for spine in ["top","left","right"]:
    ax.spines[spine].set_color('none')

ax.spines["bottom"].set_linewidth(1)
ax.tick_params(direction="out",left=False)
ax.tick_params()
ax.set_xlabel('Seconds Since Start',fontsize=15)
ax.set_ylabel('Project Name ',fontsize=15)
ax.set_yticks([15-2.5, 25-2.5, 35-2.5,45-2.5])
ax.set_yticklabels(['Project_1', 'Project_2', 'Project_3',"Project_4"])
ax.grid(linestyle="-",linewidth=.5,color="gray",alpha=.6)
ax.set_title("Example Of Matplotlib.broken_barh()",pad=10,fontsize=16,fontweight="bold")
ax.legend(frameon=False,ncol=4,loc = "lower center",bbox_to_anchor=(0.5, -.3))
ax.text(.85,.05,'Visualization by DataCharm',transform = ax.transAxes,
            ha='center', va='center',fontsize = 9,color='black')

Example Of Gantt Chart in Matplotlib

这里,我们通过贴出如下代码,帮助小伙伴们更好的理解ax.broken_barh() 函数,如下:

代码语言:javascript复制
broken_barh([(start_time, duration)],
             (lower_yaxis, hieght),
            facecolors=('tab:colours'))

甘特图(Gantt Chart)R绘制

R绘制甘特图(Gantt Chart) 我们还是首要选择ggplot2绘制,案例如下:

「ggplot2绘制」

代码语言:javascript复制
library(tidyverse)
library(ggtext)
library(hrbrthemes)
library(wesanderson)
library(LaCroixColoR)
library(RColorBrewer)
library(ggsci)
# 构建数据
data <- data.frame(name = c('A', 'B', 'C', 'D','E','F'), 
                   start = c(4, 7, 12, 16,10,8),
                   end = c(12, 11, 8, 10,13,14),
                   shift_type = c('Early', 'Mid_day', 'Mid_day', 'Late','Mid_day','Early'))
# 数据可视化
ggplot(data, aes(x=start, xend=end, y=name, yend=name, color=shift_type))  
  geom_segment(size=8,lineend = "round")  
  ggsci::scale_color_nejm()  
  labs(
    title = "Example of <span style='color:#D20F26'>ggplot2::Gantt Chart Charts</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>geom_segment()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>")  
  #hrbrthemes::theme_ft_rc(base_family = "Roboto Condensed")   
  hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")   
  theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
    plot.caption = element_markdown(face = 'bold',size = 12)
      )

Example Of Gantt Chart in base ggplot2

「ganttrify绘制」

代码语言:javascript复制
library("ganttrify")
ganttrify(project = ganttrify::test_project,
          project_start_date = "2021-03",
          font_family = "Roboto Condensed")  
    labs(
        title = "Example of <span style='color:#D20F26'>ganttrify::Gantt Chart Charts</span>",
        subtitle = "processed charts with <span style='color:#1A73E8'>geom_segment()</span>",
        caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>")  
      theme(
        plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
                                      size = 20, margin = margin(t = 1, b = 12)),
        plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
        plot.caption = element_markdown(face = 'bold',size = 12)
          )

Example01 Of Gantt Chart in ganttrify

「样例二」

代码语言:javascript复制
ganttrify(project = ganttrify::test_project,
          project_start_date = "2021-03",
          font_family = "Roboto Condensed")  
    labs(
        title = "Example of <span style='color:#D20F26'>ganttrify::Gantt Chart Charts</span>",
        subtitle = "processed charts with <span style='color:#1A73E8'>geom_segment()</span>",
        caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>")  
      theme(
        plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
                                      size = 20, margin = margin(t = 1, b = 12)),
        plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
        plot.caption = element_markdown(face = 'bold',size = 12)
          )

Example02 Of Gantt Chart in ganttrify

注意:R-ganttrify包专门绘制绘制甘特图,其默认的排版和格式都是符合一般的审美的,其还有很多用于定制化的参数,小伙伴们可以多使用哈,更多详细内容可参照R-ganttrify包[1]

总结

今天小编给大家简单的介绍甘特图(Gantt Chart) 的绘制方法,希望对大家有所帮助。当然,可能使用Excel绘制会更方便些~,但还是希望大家可以多学习一个技能的哈~~

参考资料

[1]R-ganttrify包官网: https://github.com/giocomai/ganttrify。

0 人点赞