查看原文
其他

小魔方不想跟你说话,并向你扔了一堆代码~~~

2017-02-25 小魔方 数据小魔方

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


library(ggplot2)

library(reshape2)

library(ggmap)

library(showtext)

library(Cairo)


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")


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

mydata2<-mydata

mydata2$zhibiao<-round(runif(5,1,10),0)

mydata2$zhibiao2<-round(runif(5,5,9),0)


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()


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


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()




翻倒:


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()



带目标标识点的子弹图:


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()

翻倒:


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弄熟了,未来一片光明哈哈哈~~~~




欢迎关注魔方学院QQ群


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

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