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

时间:2022-05-08
本文章向大家介绍用ggplot2画了一个我也叫不上名的炫酷图表,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

今日心血来潮,看到一幅制作精良的图表,就想使用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 & CustomernReferrals"),
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 createnlousy sales opprunities.When analyzing sharenof closed-won vs.closed-lost opportunities,nevents,leads lists and partners seem to provide thenworst performance,while refreals and socialnprovide 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()

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