仿ECO事件圆环图

该案例取自刘万祥老师的博客——EexclPro。最初使用Excel完成,相当复杂,我自己是没有那个水平了,所以本例使用R语言完成。

还原度相对来说比较高,但是该图实用性确实不很高,经济学人也只是偶尔亮出来装点门面而已。

加载包:

1
2
3
4
5
6
7
8
9
library("ggplot2")
library("Cairo")
library("ggmap")
library("xlsx")
library("lubridate")
library("reshape2")
library("showtext")
library("grid")
library("plyr")

案例图:

经济学人版

one

刘老师版

two

关键步骤:

1
2
setwd("F:/微信公众号/公众号——数据小魔方/2017年4月/20170403")
ECOdata<-read.xlsx("ECOCircle.xlsx",sheetName="Sheet1",header=T,encoding="UTF-8",stringsAsFactors=FALSE)
1
2
3
4
5
6
f<-c(99,91,84,77)
for (i in 1:nrow(ECOdata)){
m<-sum(ECOdata[i,3:6]==1)
h<-grep(1,ECOdata[i,3:6])
ECOdata[i,h+2]<-f[1:m]
}

创建时间轴:

1
2
newdate<-seq(from=as.Date("2014-01-01"),to=as.Date("2014-12-31"),by="1 day")
newmonth<-month(newdate)
1
2
3
4
5
mydata<-data.frame(newdate,newmonth);mydata$ID<-1:nrow(mydata)
mydata$Type<-ifelse(mydata$newmonth%%2==0,"A","B")
mydata$Scale<-100
a<-seq(from=90,to=-90,length=181);b<-seq(from=90,to=-90,length=184)
mydata$Circle<-c(a,b)

合并数据:

1
2
mynewdata<-merge(mydata,ECOdata,by.x="newdate",by.y="date",all.x=T)
mynewdataone<-melt(na.omit(mynewdata), id.vars =names(mynewdata)[1:7],variable.name = "Class", value.name = "Fact")
1
2
circlemonth<-seq(15,345,length=12)
circlebj<-rep(c(-circlemonth[1:3],rev(circlemonth[1:3])),2)
1
2
3
4
mynewdataoneA<-mynewdataone[mynewdataone$Fact!=0&mynewdataone$ID<180,]
mynewdataoneA<-mynewdataoneA[!duplicated(mynewdataoneA[,1]),]
mynewdataoneB<-mynewdataone[mynewdataone$Fact!=0&mynewdataone$ID>180,]
mynewdataoneB<-mynewdataoneB[!duplicated(mynewdataoneB[,1]),]

可视化图形:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
font.add("myfont","msyhl.ttc")
CairoPNG(file="ECOCircle.png",width=1000,height=1050)
showtext.begin()
ggplot()+
geom_bar(data=mydata[mydata$Type=="A",],aes(x=ID,y=Scale),stat="identity",width=1,fill="#ECEDD1",col="#ECEDD1")+
geom_bar(data=mydata[mydata$Type=="B",],aes(x=ID,y=Scale),stat="identity",width=1,fill="#DFE0B1",col="#DFE0B1")+
geom_point(data=mynewdataone[mynewdataone$Fact!=0,],aes(x=ID,y=Fact,fill=Class),size=5,shape=21,col="white")+
ylim(-90,130)+
scale_fill_manual(limits=c("Legislative","Referendum","President","Primary"),values=c("#93A299","#CF543F","#B5AE53","#86825B"),labels=c("立法","公投","总统","初选"))+
geom_text(data=mynewdataoneA,aes(x=ID,y=max(mynewdataoneA$Fact)+20,label=State,angle=Circle),family="sans",size=5,hjust=0)+
geom_text(data=mynewdataoneB,aes(x=ID,y=max(mynewdataoneB$Fact)+20,label=State,angle=Circle),family="sans",size=5,hjust=1)+
geom_text(data=NULL,aes(x=circlemonth,y=30,label=paste0(1:12,"月"),angle=circlebj),family="myfont",size=7,hjust=.5,vjust=.5)+
guides(colour=guide_legend(title=NULL))+
coord_polar(theta="x")+
labs(title="2014年全球选举事件图",subtitle="这是一幅用心良苦的好图",caption="Source:Economics\nMake:EasyCharts",x="",y="",fill="")+
theme(
text=element_text(family="myfont"),
axis.text=element_blank(),
axis.ticks=element_blank(),
panel.background=element_blank(),
panel.grid=element_blank(),
panel.border=element_blank(),
legend.position=c(0.03,0.92),
legend.background=element_blank(),
legend.key=element_blank(),
legend.key.size=unit(1.55,'cm'),
legend.key.height=unit(1.2,'cm'),
legend.text=element_text(size=20,hjust=3,vjust=3,face="bold"),
plot.background=element_blank(),
plot.title=element_text(size=50),
plot.subtitle=element_text(size=35),
plot.caption=element_text(size=25,hjust=0),
plot.margin=unit(c(.5,.5,.5,.5),"lines"),
)
showtext.end()
dev.off()

three

four


联系方式:
wechat:ljty1991
Mail:578708965@qq.com
个人公众号:数据小魔方(datamofang)

qq交流群:[魔方学院]298236508

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


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

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