超强脑洞第一弹——子弹图~~~

很早就跟大家说过,ggplot是一个黑洞,哈神是一个艺术家,真正体会到ggplot的强大,也就是近几个月的事儿,只要你的灵感不受束缚,它真的……无所不能,今天看一个商务场合经常使用的,绩效与目标管理工具——子弹图。

数据准备:

导入包:

1
2
3
4
5
library(ggplot2)
library(reshape2)
library(ggmap)
library(showtext)
library(Cairo)

构建作图数据:

1
2
3
4
5
6
7
8
9
10
KPI<-c("KPI1","KPI2","KPI3","KPI4","KPI5")
INDEX<-1:5
good<-rep(0.2,5)
excellent<-good
pass<-rep(0.6,5)
target<-c(0.84,0.85,0.7,0.92,0.78)
fact<-c(0.91,0.8,0.68,0.91,0.8)
mydata<-data.frame(KPI,INDEX,excellent,good,pass,target,fact)
mydata1<-melt(mydata,id.vars=c("KPI","target","INDEX","fact"),variable.name="perform",value.name="scope")
color<-c("#43546C","#8695B2","#D9DBDF")

这个是第一张图的数据源:双柱图

1
2
3
mydata2<-mydata
mydata2$zhibiao<-round(runif(5,1,10),0)
mydata2$zhibiao2<-round(runif(5,5,9),0)

1
2
3
4
5
6
7
8
9
CairoPNG(file="C:/Users/Administrator/Desktop/dbq.png",width=300,height=400)
ggplot()+
geom_bar(data=mydata2,aes(x=KPI,y=zhibiao),stat="identity",width=.3,fill="#43546C",col=NA)+
geom_bar(data=mydata2,aes(x=KPI,y=zhibiao2),stat="identity",width=.7,fill=NA,col="#43546C")+
ylim(-2,15)+
geom_text(data=NULL,aes(x=1:5,y=rep(-1,5),label=KPI))+
geom_text(data=mydata2,aes(x=KPI,y=zhibiao,label=zhibiao),vjust=-.5)+
theme_nothing()
dev.off()

不带目标标识点的子弹图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CairoPNG(file="C:/Users/Administrator/Desktop/db.png",width=300,height=400)
ggplot()+
geom_bar(data=mydata1,aes(x=INDEX,y=scope,fill=perform),stat="identity",position="stack",width=.7)+
ylim(-0.15,1.2)+
geom_linerange(data=mydata1,aes(x=INDEX,ymin=0,ymax=fact),col="#000002",size=5)+
scale_fill_manual(values=sort(color,decreasing=T) ) +
geom_errorbarh(data=mydata,aes(x=INDEX,y=target,xmax =INDEX+.25, xmin =INDEX-.25),height=0,size=1.5,colour="#BA262A")+
guides(fill=guide_legend(title=NULL))+
geom_text(data=NULL,aes(x=1:5,y=rep(-.05,5),label=KPI))+
theme_nothing(legend=TRUE)+
theme(
legend.direction="horizontal",
legend.position=c(0.5,.88),
legend.text=element_text(size=12)
)
dev.off()

翻倒:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CairoPNG(file="C:/Users/Administrator/Desktop/db11.png",width=400,height=300)
ggplot()+
geom_bar(data=mydata1,aes(x=INDEX,y=scope,fill=perform),stat="identity",position="stack",width=.7)+
ylim(-0.15,1)+
coord_flip()+
scale_x_reverse()+
geom_linerange(data=mydata1,aes(x=INDEX,ymin=0,ymax=fact),col="#000002",size=5)+
scale_fill_manual(values=sort(color,decreasing=T) ) +
geom_errorbarh(data=mydata,aes(x=INDEX,y=target,xmax =INDEX+.25, xmin =INDEX-.25),height=0,size=1.5,colour="#BA262A")+
guides(fill=guide_legend(title=NULL))+
geom_text(data=NULL,aes(x=1:5,y=rep(-.1,5),label=KPI))+
theme_nothing(legend=TRUE)+
theme(
legend.text=element_text(size=12)
)
dev.off()

带目标标识点的子弹图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CairoPNG(file="C:/Users/Administrator/Desktop/db1.png",width=300,height=400)
ggplot()+
geom_bar(data=mydata1,aes(x=KPI,y=scope,fill=perform),stat="identity",position="stack",width=.7)+
ylim(-.15,1.2)+
geom_linerange(data=mydata1,aes(x=KPI,ymin=0,ymax=fact),col="#000002",size=5)+
scale_fill_manual(values=sort(color,decreasing=T) ) +
geom_errorbarh(data=mydata,aes(x=KPI,y=target,xmax =KPI+.25, xmin =KPI-.25),height=0,size=1.5,colour="#BA262A")+
geom_text(data=NULL,aes(x=1:5,y=rep(-.05,5),label=KPI))+
theme_nothing(legend=TRUE)+
theme(
legend.direction="horizontal",
legend.position=c(0.5,.88),
legend.text=element_text(size=12)
)
dev.off()

翻倒:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CairoPNG(file="C:/Users/Administrator/Desktop/db13.png",width=400,height=300)
ggplot()+
geom_bar(data=mydata1,aes(x=KPI,y=scope,fill=perform),stat="identity",position="stack",width=.7)+
ylim(-.15,1)+
coord_flip()+
geom_linerange(data=mydata1,aes(x=KPI,ymin=0,ymax=fact),col="#000002",size=5)+
scale_fill_manual(values=sort(color,decreasing=T))+
geom_errorbarh(data=mydata,aes(x=KPI,y=target,xmax =KPI+.25, xmin =KPI-.25),height=0,size=1.5,colour="#BA262A")+
geom_text(data=NULL,aes(x=1:5,y=rep(-.1,5),label=KPI))+
theme_nothing(legend=TRUE)+
theme(
legend.text=element_text(size=12)
)+
guides(fill=guide_legend(title=NULL))
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 国际许可协议进行许可。

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