查看原文
其他

超强脑洞第四弹——ggplot构造甘特图

2017-03-01 小魔方 数据小魔方

甘特图即便是用excel来做,也是要吃些苦头的,ggplot中无此内置图层函数,但是,没啥图能难道哈神的巨作,ggplot制作甘特不费吹灰之力,而且还能做得有模有样,有板有眼。以下是代码过程。


library("lubridate")

library("ggplot2")

library("ggmap")

library(showtext)

library(grid)

library(scales)

library(Cairo)


数据集构造:


Item<-paste("Step"," ",1:8,sep="")

Planned_Start_Date<-c("2016/03/03","2016/03/16","2016/03/28","2016/04/02","2016/04/12","2016/04/22","2016/05/16","2016/05/22")

Planned_Finish_Date<-c("2016/03/15","2016/03/31","2016/04/04","2016/04/15","2016/04/26","2016/05/20","2016/05/28","2016/06/12")

Actual_Start_Date<-c("2016/03/03","2016/03/16","2016/03/27","2016/04/05","2016/04/13","2016/04/22","2016/05/16","2016/05/22")

Actual_Finish_Date<-c("2016/03/18","2016/03/28","2016/04/05","2016/04/16","2016/04/27","2016/05/15","2016/05/16","2016/05/22")


mydata<-data.frame(Item,Planned_Start_Date,Planned_Finish_Date,Actual_Start_Date,Actual_Finish_Date,stringsAsFactors = FALSE)


日期变量转换:


mydata$Planned_Start_Date<-ymd(mydata$Planned_Start_Date)

mydata$Planned_Finish_Date<-ymd(mydata$Planned_Finish_Date)

mydata$Actual_Start_Date<-ymd(mydata$Actual_Start_Date)

mydata$Actual_Finish_Date<-ymd(mydata$Actual_Finish_Date)



datebreaks<-seq(as.Date("2015-03-01"),as.Date("2015-06-01"),by="1 month")

time<-as.Date("2016-05-15")



GGsave函数渲染输出:

windowsFonts(myFont = windowsFont("微软雅黑"))

p<-ggplot()+

geom_linerange(data=mydata,aes(x=Item,ymin=Planned_Start_Date,ymax=Planned_Finish_Date),size=10,color="#BFBFBF",alpha=0.8)+

geom_linerange(data=mydata,aes(x=Item,ymin=Actual_Start_Date,ymax=Actual_Finish_Date),size=7,color="#085264",alpha=0.8)+

scale_x_discrete(limits=sort(Item,decreasing=T))+

scale_y_date(position ="top")+

#scale_y_date(breaks=datebreaks,labels=date_format("%Y %b"))+

#geom_hline(data=NULL,aes(hintercept=time))+

coord_flip()+

theme(

axis.title=element_blank(),

axis.text.x=element_text(margin=margin(5,0,0,0,"pt")),

axis.text.y=element_text(margin=margin(0,10,0,0,"pt")),

axis.ticks.y=element_blank(),

panel.grid.major.y=element_line(color="#FFB666",linetype=5),

panel.background=element_rect(fill="white"),

axis.text=element_text(colour ="black",size=10,face="italic",family="myFont"),

axis.line.x=element_line(),

panel.spacing=unit(-0.3,"cm")

)

ggsave("C:/Users/Administrator/Desktop/甘特图.png",p,width=140,height=75,unit="mm",dpi=100) 




Cairo高清渲染输出:

font.add("myfont","msyhl.ttc")

CairoPNG(file="C:/Users/Administrator/Desktop/Gante.png",width=600,height=300)

showtext.begin()

ggplot()+

geom_linerange(data=mydata,aes(x=Item,ymin=Planned_Start_Date,ymax=Planned_Finish_Date),size=10,color="#BFBFBF",alpha=0.8)+

geom_linerange(data=mydata,aes(x=Item,ymin=Actual_Start_Date,ymax=Actual_Finish_Date),size=7,color="#085264",alpha=0.8)+

scale_x_discrete(limits=sort(Item,decreasing=T))+

scale_y_date(position ="top")+

#scale_y_date(breaks=datebreaks,labels=date_format("%Y %b"))+

#geom_hline(data=NULL,aes(hintercept=time))+

coord_flip()+

theme(

axis.title=element_blank(),

axis.text.x=element_text(margin=margin(5,0,0,0,"pt")),

axis.text.y=element_text(margin=margin(0,10,0,0,"pt")),

axis.ticks.y=element_blank(),

panel.grid.major.y=element_line(color="#FFB666",linetype=5),

panel.background=element_rect(fill="white"),

axis.text=element_text(colour ="black",size=10,face="italic",family="myfont"),

axis.line.x=element_line(),

panel.spacing=unit(-0.3,"cm")

)

showtext.end()

dev.off()



核心要点总结:


  • 数据构造:本案例使用geom图层系统中的范围线图层来临摹的(linerange),该图层接受两个变量的范围(起点、终点),因为有计划日期、实际执行日期,所以使用了两个linerange图层,四个变量(计划开始日期、计划结束日期、实际开始日期、实际结束日期)。数据构造主要涉及日期变量转换。

  • 图层映射过程,掌握好linerange图层内的元素调整(颜色、线条宽度、类型等)。


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存