前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >仿经济学人——矩阵气泡图

仿经济学人——矩阵气泡图

作者头像
数据小磨坊
发布2018-04-11 17:37:01
2.2K0
发布2018-04-11 17:37:01
举报
文章被收录于专栏:数据小魔方数据小魔方

本篇文章案例来源于经济学人2013年一幅关于家庭支出结构与国家间的交叉对比图。

该图信息量相当丰富,至少涵盖了四个维度的信息,支出结构信息(类别型字段)、国别信息(类别型字段)、支出水平分类(类别型字段)、支出规模(数值型指标)等。

倘若使用ggplot进行绘制,思路非常简单,仅通过散点图层皆可完成,ggplot2的散点图可以支持离散标度,但是如果想要处理好类别的顺序,需要把类别型变量因子化。

因为原图中有奖金100+数据点,很多都没有具体数值,一个一个用肉眼估计简直丧心病狂,所以我模拟了一组数值,只保证思路讲到位就OK。

代码语言:javascript
复制
setwd("E:/微信公众号/公众号——数据小魔方/2017年9月/20170914/")
mydata<-read.csv("matrix_bubble.csv",stringsAsFactors = FALSE,check.names = FALSE)

原始数据中带有\n,导入时R语言会自动给\添加一个\,这里涉及到R语言中保留字符的问题,需要将多余的\删掉。

代码语言:javascript
复制
mydata$Class[c(1,6,7)]<-c("Housing,fuel\n&utilities","Restaurants\n& hotels","Clothing\n& footwear")
names(mydata)[9:11]<-c("Saudi\nArabia","South\nKorea","United\nStates")library("tidyr")
mydata1<-gather(mydata,Country,Spend,-1)
mydata1$Class<-factor(mydata1$Class,levels=c("Education","Alcohol & tobacco","Communications","Furnishings","Clothing\n& footwear","Restaurants\n& hotels","Health","Recreation","Transport","Food","Housing,fuel\n&utilities"),ordered=T)

分割支出规模的类别区间:

代码语言:javascript
复制
qa<-quantile(mydata1$Spend,c(0,.25,.5,.75,1))
mydata1$Spend_fact<-cut(mydata1$Spend,breaks=qa,labels = c("lowest spend","below average","above average","highest spend"),include.lowest=TRUE,ordered=T)

制作草图:

代码语言:javascript
复制
library("ggplot2")library("grid")library("showtext")library("Cairo")
font.add("myfont","msyh.ttc")
代码语言:javascript
复制
setwd("E:/《R语言商务图表与可视化》/9.12——R语言ggplot2可视化在线分享")
CairoPNG(file="matirx_scatter.png",width=1200,height=900)
showtext.begin()
ggplot(data=mydata1)+

geom_hline(aes(x=Country,y=Class,yintercept = 1:nrow(mydata1)),size=20,colour="#E4EDF2",alpha=.5)+
geom_vline(aes(x=Country,y=Class,xintercept = 1:nrow(mydata1)),linetype="dashed")+
geom_point(aes(x=Country,y=Class,size=Spend,fill=Spend_fact),shape=21,colour="white")+
scale_fill_manual(values=c("#F9DBD3","#F1B255","#519F46","#41B0C3"))+
scale_size_area(max_size=25)+
scale_x_discrete(position = "top")+
guides(size=FALSE,fill=guide_legend(title="Within category",direction="horizontal"))+
labs(title="How they spend it",subtitle="Househlod spending*,of total,2013 or latest,includes taxes",caption="Source:Eurostat")+
theme_void(base_size=20,base_family="myfont") %+replace%
theme(
      legend.position="top",
      panel.grid.major.x=element_line(linetype="dashed"),      #plot.margin=margin(5,5,5,5,unit="pt"),
      axis.text=element_text(size=15,hjust=0.5),
      plot.title=element_text(size=35,hjust=0,lineheight=1.2),
      plot.caption=element_text(hjust=0,lineheight=1.2)
) 
showtext.end()
dev.off()

数据源请移步GitHub主页下载: https://github.com/ljtyduyu/DataWarehouse/tree/master/File

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-09-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据小魔方 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档