超强脑洞第二弹之——ggplot构造漏斗图

今天这篇要用ggplot构造漏斗图,其实ggplot内置图层函数中不存在所谓的漏斗图、子弹图等比较复杂的图表类型,但是ggplot的现有图层函数和标度设置完全可以胜任这些图形,以下是利用ggplot临摹漏斗图的代码过程。

数据准备:

1
2
3
library(reshape2)
library(plyr)
library(ggplot2)
1
2
3
4
5
6
7
scope<-c(0.9,0.8,0.6,0.4,0.2)
Part<-paste("part",1:5,sep="")
Order<-1:5
help<-(1-scope)/2
mydata<-data.frame(Order,Part,help,scope)
mydata1<-melt(mydata,id.vars=c("Order","Part"),variable.name="perform",value.name="scope")
mydata1$perform<-factor(mydata1$perform,level=c("scope","help"),order=T)

很重要的一步,需要构造有序因子变量,两个因子水平,分别是实际指标值和辅助值,在构造有序因子变量时,注意辅助值因子水平要高于实际值数据。柱形图堆叠时,按照因子水平由高到低堆叠(底层因子水平高,顶层因子水平低,这样才能将指标值的数据条撑起,其实水平均居中)。

可视化过程:

1
ggplot(mydata1,aes(Order,scope,order=desc(scope),fill=perform))+geom_bar(stat="identity",position="stack")

制作色盘:
(其实使用了一个白色色值隐藏掉了辅助列,理念跟在excel里面制作漏斗图一致,但是色盘颜色顺序白色要在第一个,这样将来颜色映射的时候颜色顺序与因子水平由大到小进行匹配的。)这一点非常重要,也是ggplot临摹漏斗图的核心技巧。

1
2
3
4
5
6
7
8
9
Color<-c("#FFFFFF","#088158")
CairoPNG(file="C:/Users/Administrator/Desktop/漏斗图1.png",width=330,height=400)
ggplot()+
geom_bar(data=mydata1,aes(x=Order,y=scope,fill=perform),stat="identity",position="stack")+
scale_fill_manual(values=sort(Color))+
geom_text(data=mydata,aes(x=Order,y=help+scope/2-.025,label=Part),col="white",size=4)+
geom_text(data=mydata,aes(x=Order,y=help+scope/2+.035,label=paste(100*mydata$scope,"%",sep="")),col="white",size=5.5)+
theme_nothing()
dev.off()

转向(反转坐标轴)

1
2
3
4
5
6
7
8
9
CairoPNG(file="C:/Users/Administrator/Desktop/漏斗图2.png",width=330,height=400)
ggplot()+
geom_bar(data=mydata1,aes(x=Order,y=scope,fill=perform),stat="identity",position="stack")+
scale_fill_manual(values=sort(Color))+
scale_x_reverse()+
geom_text(data=mydata,aes(x=Order,y=help+scope/2-.025,label=Part),col="white",size=4)+
geom_text(data=mydata,aes(x=Order,y=help+scope/2+.035,label=paste(100*mydata$scope,"%",sep="")),col="white",size=5.5)+
theme_nothing()
dev.off()

水平漏斗图:

1
2
3
4
5
6
7
8
9
CairoPNG(file="C:/Users/Administrator/Desktop/漏斗图3.png",width=330,height=400)
ggplot()+
geom_bar(data=mydata1,aes(x=Order,y=scope,fill=perform),stat="identity",position="stack")+
scale_fill_manual(values=sort(Color))+
coord_flip()+
geom_text(data=mydata,aes(x=Order,y=help+scope/2-.04,label=Part),col="white",size=4)+
geom_text(data=mydata,aes(x=Order,y=help+scope/2+.04,label=paste(100*mydata$scope,"%",sep="")),col="white",size=5.5)+
theme_nothing()
dev.off()

反转坐标轴:

1
2
3
4
5
6
7
8
9
10
CairoPNG(file="C:/Users/Administrator/Desktop/漏斗图4.png",width=330,height=400)
ggplot()+
geom_bar(data=mydata1,aes(x=Order,y=scope,fill=perform),stat="identity",position="stack")+
scale_fill_manual(values=sort(Color))+
coord_flip()+
scale_x_reverse()+
geom_text(data=mydata,aes(x=Order,y=help+scope/2-.05,label=Part),col="white",size=4)+
geom_text(data=mydata,aes(x=Order,y=help+scope/2+.05,label=paste(100*mydata$scope,"%",sep="")),col="white",size=5.5)+
theme_nothing()
dev.off()

本文小结:

下面总结以下使用ggplot临摹漏斗图的核心技巧:

  • 指标值和辅助列值这两个因子水平的设置上,需要设置成有序因子,因子水平大小为指标值因子水平<辅助值因子水平。因为柱状图堆叠时因子水平由大到小从底部顺次向顶部堆积。这样辅助列可以堆在底部,刚好撑起数据列,将其置于水平居中位置。因此模拟漏斗图。
  • 色盘设置,色值顺序白色在前,数值色在后。颜色映射时,色板颜色会顺次分配给由高到低的因子水平。(其实因为就两个颜色,即便是颜色色值写反了,使用逆序函数倒过来就好了)。

好了,期待下一篇ggplot的脑洞吧,可能是甘特图,也有可能是瀑布图,或者其他不知名的图表~


联系方式:
wechat:ljty1991
Mail:578708965@qq.com
个人公众号:数据小魔方(datamofang)
团队公众号:EasyCharts
qq交流群:[魔方学院]298236508

个人简介:
杜雨
财经专业研究僧;
伪数据可视化达人;
文科背景的编程小白;
喜欢研究商务图表与地理信息数据可视化,爱倒腾PowerBI、SAP DashBoard、Tableau、R ggplot2、Think-cell chart等诸如此类的数据可视化软件,创建并运营微信公众号“数据小魔方”。
Mail:578708965@qq.com


备注信息:
知识共享许可协议
本作品采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可。

坚持原创技术分享,您的支持将鼓励我继续创作!