查看原文
其他

用ggplot2画了一个我也叫不上名的炫酷图表

2017-09-12 杜雨 EasyCharts

今日心血来潮,看到一幅制作精良的图表,就想使用ggplot2代码实现,虽然不知道该怎么称呼这个图表,但是能顺利做出来也是很有成就感的!

加载数据包

library("ggplot2")
library("grid")
library("showtext")
library("Cairo") font.add("myfont","msyh.ttc")

构造图形数据源

mydata<-data.frame( id=1:13, class=rep_len(1:4, length=13), Label=c("Events","Lead List","Partner","Markeiting & Advertising","Tradeshows","Paid Search","Webinar","Emial Campaign","Sales generated","Website","Other","Facebook/Twitter/\nOther Social","Employee & Customer\nReferrals"), Value=c(7.6,15.5,17.9,21.8,29.6,29.7,32.7,43.0,57.5,61.4,67.4,68.6,68.7) )

可视化过程:

第一步:制作基本柱形图:

(这里我用一个序列作为 占位遮挡住了底部的堆积柱形图)

ggplot(mydata)+ geom_col(aes(x=id,y=Value/2+150,fill=factor(class)),colour=NA,width=1)+ geom_col(aes(x=id,y=150-Value/2),fill="white",colour="white",width=1)+ scale_x_continuous(limits=c(0,26),expand=c(0,0))

第二步:使用极坐标转换:

ggplot(mydata)+ geom_col(aes(x=id,y=Value/2+150,fill=factor(class)),colour=NA,width=1)+ geom_col(aes(x=id,y=150-Value/2),fill="white",colour="white",width=1)+ scale_x_continuous(limits=c(0,26),expand=c(0,0))+ coord_polar(theta = "x",start=-14.275, direction = 1)

第三步:对颜色搭配和主题进行修饰:

ggplot(mydata)+ geom_col(aes(x=id,y=Value/2+150,fill=factor(class)),colour=NA,width=1)+ geom_col(aes(x=id,y=150-Value/2),fill="white",colour="white",width=1)+ scale_x_continuous(limits=c(0,26),expand=c(0,0))+ coord_polar(theta = "x",start=-14.275, direction = 1)+ scale_fill_manual(values=c("#31A2CE","#DDB925","#3F9765","#C84F44"),guide=FALSE)+ theme_void()

第四步:修饰剩余文本元素:

p<-ggplot()+ geom_col(data=mydata,aes(x=id,y=Value/2+150,fill=factor(class)),colour=NA,width=1)+ geom_col(data=mydata,aes(x=id,y=150-Value/2),fill="white",colour="white",width=1)+ geom_line(data=NULL,aes(x=rep(c(.5,13.5),2),y=rep(c(126,174),each=2),group=factor(rep(1:2,each=2))),linetype=2,size=.25)+ geom_text(data=mydata,aes(x=id,y=ifelse(id<11,160,125),label=Label),size=3.5,hjust=0.5)+ geom_text(data=mydata,aes(x=id,y=ifelse(id<11,185,150),label=paste0(Value,"%")),hjust=.5,size=4.5)+ scale_x_continuous(limits=c(0,26),expand=c(0,0))+ coord_polar(theta = "x",start=-14.275, direction = 1)+ scale_fill_manual(values=c("#31A2CE","#DDB925","#3F9765","#C84F44"),guide=FALSE)+ theme_void();p

第五步:精修图表

#图表标题、副标题title="Events,Lead Lists and partners-\nmore likely be colosed-lost"content="Marketing events may by fun, but they create\nlousy sales opprunities.When analyzing share\nof closed-won vs.closed-lost opportunities,\nevents,leads lists and partners seem to provide the\nworst performance,while refreals and social\nprovide the best performance."#图形输出:setwd("E:/数据可视化/R/R语言学习笔记/数据可视化/ggplot2/优秀R语言案例") CairoPNG(file="polar_bar.png",width=1200,height=900) showtext.begin() grid.newpage() pushViewport(viewport(layout=grid.layout(6,8))) vplayout<-function(x,y){viewport(layout.pos.row =x,layout.pos.col=y)} print(p,vp=vplayout(1:6,1:8)) grid.text(label=title,x=.50,y=.6525,gp=gpar(col="black",fontsize=15,fontfamily="myfont",draw=TRUE,fontface="bold",just="left")) grid.text(label=content,x=.50,y=.56,gp=gpar(col="black",fontsize=12,fontfamily="myfont",draw=TRUE,just="left")) showtext.end() dev.off()

备注:(以上图表标签是手动调整过位置的)

好了干货完了,下面是一波广告:


9月12日晚8~10点,本小编有一场关于ggplot2的微课,主要内容如下:


1、ggplot2图层语法的核心理念
2、ggplot函数与geom_xxx函数间的父子继承关系
3、美学映射参数写在ggplot函数内与写在geom_xxx内的差异
4、美学映射参数写在aes函数内部和写在aes函数外部的差异
5、颜色标度一共有几种类型和写法,在不同模块中是否能够共用
6、如何结合实际业务与引用场景进行颜色标度选择
7、多图层叠加时,如何解决颜色标度冲突的问题
8、分面函数的权限控制
9、主题框架与模块间的继承关系
10、主题函数更新与替换方案
11、图形输出与高清抗锯齿渲染
其实这些问题都是之前我学习过程中走过的弯路,随着练习的案例越来越多,这些问题一步步全都解决了,其实如果你能有心看完我的所有关于ggplot讲解部分,差不多这些问题也都能全部理解。

阅读原文报名:



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

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