R语言可视化(二十二):甘特图绘制

22. 甘特图绘制


清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/22gantt/")

使用plotrix包绘制甘特图

# 安装并加载所需的R包
#install.packages("plotrix")
library(plotrix)

# 构建测试数据集
Ymd.format<-"%Y/%m/%d"
gantt.info <-list(labels= c("First task","Second task","Third task","Fourth task","Fifth task"),
                  starts= as.POSIXct(strptime(c("2004/01/01","2004/02/02","2004/03/03","2004/05/05","2004/09/09"),format=Ymd.format)),
                  ends= as.POSIXct(strptime(c("2004/03/03","2004/05/05","2004/05/05","2004/08/08","2004/12/12"),format=Ymd.format)),
                  priorities=c(1,2,3,4,5)
                  )
gantt.info
## $labels
## [1] "First task"  "Second task" "Third task"  "Fourth task" "Fifth task" 
##
## $starts
## [1] "2004-01-01 CST" "2004-02-02 CST" "2004-03-03 CST" "2004-05-05 CST"
## [5] "2004-09-09 CST"
##
## $ends
## [1] "2004-03-03 CST" "2004-05-05 CST" "2004-05-05 CST" "2004-08-08 CST"
## [5] "2004-12-12 CST"
##
## $priorities
## [1] 1 2 3 4 5

vgridpos <- as.POSIXct(strptime(c("2004/01/01","2004/02/01","2004/03/01","2004/04/01",
                                  "2004/05/01","2004/06/01","2004/07/01","2004/08/01",
                                  "2004/09/01","2004/10/01","2004/11/01","2004/12/01"),
                                format=Ymd.format))
vgridpos
## [1] "2004-01-01 CST" "2004-02-01 CST" "2004-03-01 CST" "2004-04-01 CST"
## [5] "2004-05-01 CST" "2004-06-01 CST" "2004-07-01 CST" "2004-08-01 CST"
## [9] "2004-09-01 CST" "2004-10-01 CST" "2004-11-01 CST" "2004-12-01 CST"

vgridlab <- c("Jan","Feb","Mar","Apr","May","Jun",
              "Jul","Aug","Sep","Oct","Nov","Dec")
vgridlab
##  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

# 使用gantt.charth函数绘制甘特图
gantt.chart(gantt.info, # a list of task labels, start/end times and task priorities
            main="Calendar date Gantt chart (2004)", # 设置标题
            priority.legend=TRUE, # 设置是否展示color图例
            vgridpos=vgridpos, # 设置垂直网格线的位置
            vgridlab=vgridlab, # 设置垂直网格线的标签
            hgrid=TRUE # 设置是否显示水平网格线
            )
image.png
# add a little extra space on the right side
gantt.chart(gantt.info,
            main="Calendar date Gantt chart (2004)",
            priority.legend=TRUE,
            vgridpos=vgridpos,
            vgridlab=vgridlab,
            hgrid=TRUE,
            taskcolors = rainbow(5),
            priority.label = "Task priorities",
            xlim=as.POSIXct(strptime(c("2004/01/01","2004/12/20"),
                                     format=Ymd.format)))
image.png
# if both vgidpos and vgridlab are specified,
# starts and ends don't have to be dates
info2 <- list(labels=c("Jim","Joe","Jim","John","John","Jake","Joe","Jed","Jake"),
              starts=c(8.1,8.7,13.0,9.1,11.6,9.0,13.6,9.3,14.2),
              ends=c(12.5,12.7,16.5,10.3,15.6,11.7,18.1,18.2,19.0))
info2
## $labels
## [1] "Jim"  "Joe"  "Jim"  "John" "John" "Jake" "Joe"  "Jed"  "Jake"
##
## $starts
## [1]  8.1  8.7 13.0  9.1 11.6  9.0 13.6  9.3 14.2
##
## $ends
## [1] 12.5 12.7 16.5 10.3 15.6 11.7 18.1 18.2 19.0

gantt.chart(info2,vgridlab=8:19,vgridpos=8:19,
            main="All bars the same color",
            taskcolors="lightgray",
            cylindrical = T)
image.png
gantt.chart(info2,vgridlab=8:19,vgridpos=8:19,
            main="A color for each label",
            taskcolors=c(2,3,7,4,8),
            priority.legend = T)
image.png
gantt.chart(info2,vgridlab=8:19,vgridpos=8:19,
            main="A color for each interval - with borders",
            taskcolors=c(2,3,7,4,8,5,3,6,"purple"),
            border.col="black")
image.png

使用vistime包绘制甘特图

# 安装并加载所需的R包
#install.packages("vistime")
library(vistime)

# 构建测试数据集
pres <- data.frame(
  Position = rep(c("President", "Vice"), each = 3),
  Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
  start = c("1789-03-29", "1797-02-03", "1801-02-03"),
  end = c("1797-02-03", "1801-02-03", "1809-02-03"),
  color = c("#cbb69d", "#603913", "#c69c6e")
)
pres
##    Position       Name      start        end   color
## 1 President Washington 1789-03-29 1797-02-03 #cbb69d
## 2 President      Adams 1797-02-03 1801-02-03 #603913
## 3 President  Jefferson 1801-02-03 1809-02-03 #c69c6e
## 4      Vice      Adams 1789-03-29 1797-02-03 #cbb69d
## 5      Vice  Jefferson 1797-02-03 1801-02-03 #603913
## 6      Vice       Burr 1801-02-03 1809-02-03 #c69c6e

# 使用gg_vistime函数绘制甘特图
gg_vistime(pres, 
           col.event = "Position", 
           col.group = "Name", 
           col.start = "start",
           col.end = "end",
           col.color = "color",
           title = "Presidents of the USA")
image.png
sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 
[2] LC_CTYPE=Chinese (Simplified)_China.936   
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C                              
[5] LC_TIME=Chinese (Simplified)_China.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.8.3   plotrix_3.7-6

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5       pillar_1.4.2     compiler_3.6.0   tools_3.6.0     
 [5] digest_0.6.20    evaluate_0.14    tibble_2.1.3     nlme_3.1-139    
 [9] gtable_0.3.0     lattice_0.20-38  pkgconfig_2.0.2  rlang_0.4.7     
[13] rstudioapi_0.10  yaml_2.2.0       xfun_0.8         knitr_1.23      
[17] generics_0.0.2   grid_3.6.0       tidyselect_0.2.5 glue_1.3.1      
[21] R6_2.4.0         rmarkdown_1.13   ggplot2_3.2.0    purrr_0.3.2     
[25] tidyr_0.8.3      magrittr_1.5     htmltools_0.3.6  scales_1.0.0    
[29] backports_1.1.4  assertthat_0.2.1 colorspace_1.4-1 lazyeval_0.2.2  
[33] munsell_0.5.0    broom_0.5.2      crayon_1.3.4    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342