前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R可视乎|复合饼图系列

R可视乎|复合饼图系列

作者头像
庄闪闪
发布2021-04-09 10:55:33
1K0
发布2021-04-09 10:55:33
举报

散点复合饼图(compound scatter and pie chart)可以展示三个数据变量的信息:(x, y, P),其中x和y决定气泡在直角坐标系中的位置,P表示饼图的数据信息,决定饼图中各个类别的占比情况,如图(a)所示。

气泡复合饼图(compound bubble and pie chart)可以展示四个数据变量的信息:(x, y, z, P),其中x 和y 决定气泡在直角坐标系中的位置,z 决定气泡的大小,P 表示饼图的数据信息,决定饼图中各个类别的占比情况,如图(b)所示。

数据介绍

这是一个和犯罪有关的数据,主要用到里面几列数据,murder、Forcible_rate、Robbery、aggravated_assult、burglary、larceny_theft、motor_vehicle_theft。

library(ggplot2)
library(scatterpie)
library(RColorBrewer)
crime<- read.csv("~/crimeRatesByState2005.tsv",header = TRUE, sep = "\t", stringsAsFactors = F)
radius <- sqrt(crime$population / pi)
Max_radius<-max(radius)
Bubble_Scale<-0.1
crime$radius <- Bubble_Scale * radius/Max_radius
mydata<-crime[,c(2,4,3,5:8)]  #数据集构造
Col_Mean<-apply(mydata,2,mean)
Col_Sort<-sort(Col_Mean,index.return=TRUE,decreasing = TRUE)
mydata<-mydata[,Col_Sort$ix]
x<-(mydata$murder-min(mydata$murder))/(max(mydata$murder)-min(mydata$murder))+0.00001
y<-(mydata$Robbery-min(mydata$Robbery))/(max(mydata$Robbery)-min(mydata$Robbery))+0.00001
xlabel<-seq(0,10,2)
xbreak<-(xlabel-min(mydata$murder))/(max(mydata$murder)-min(mydata$murder))+0.00001
ylabel<-seq(0,260,50)
ybreak<-(ylabel-min(mydata$Robbery))/(max(mydata$Robbery)-min(mydata$Robbery))+0.00001
mydata2<-data.frame(x,y,radius=crime$radius)
mydata2<-cbind(mydata2,mydata)
Legnd_label<-colnames(mydata2)[4:10]
colnames(mydata2)[4:10]<-LETTERS[1:7]

散点复合饼图系列(a)

ggplot() + 
  geom_scatterpie(aes(x=x, y=y,r=0.05), data=mydata2, cols=colnames(mydata2)[4:10],alpha=0.9,size=0.1) +
  scale_fill_manual(values=colorRampPalette(brewer.pal(7, "Set2"))(7),labels=Legnd_label)+
  #geom_scatterpie_legend(mydata2$radius, x=0.1, y=0.95, n=5,labeller=function(x) round((x* Max_radius/ Bubble_Scale)^2*pi))+
  #geom_scatterpie_legend(mydata2$radius, x=0.009758116, y=0.090868067, n=4,labeller=function(x) round((x* Max_radius/ Bubble_Scale)^2*pi))+
  scale_x_continuous(breaks=xbreak, labels=xlabel)+
  scale_y_continuous(breaks=ybreak, labels=ylabel)+
  xlab("murder")+
  ylab("Robbery")+
  coord_fixed()+
  theme(
    axis.title=element_text(size=15,face="plain",color="black"),
    axis.text = element_text(size=13,face="plain",color="black"),
    legend.title=element_text(size=15,face="plain",color="black"),
    legend.text = element_text(size=14,face="plain",color="black")
)

散点复合饼图系列(b)

ggplot() + 
  geom_scatterpie(aes(x=x, y=y,r=radius), data=mydata2, cols=colnames(mydata2)[4:10],alpha=0.9,size=0.25) +
  scale_fill_manual(values=colorRampPalette(brewer.pal(7, "Set2"))(7),labels=Legnd_label)+
  geom_scatterpie_legend(mydata2$radius, x=0.1, y=0.95, n=5,
                         labeller=function(x) round((x* Max_radius/ Bubble_Scale)^2*pi))+
  #geom_scatterpie_legend(mydata2$radius, x=0.009758116, y=0.090868067, n=4,labeller=function(x) round((x* Max_radius/ Bubble_Scale)^2*pi))+
  scale_x_continuous(breaks=xbreak, labels=xlabel)+
  scale_y_continuous(breaks=ybreak, labels=ylabel)+
  xlab("murder")+
  ylab("Robbery")+
  coord_fixed()+
  theme(
    axis.title=element_text(size=15,face="plain",color="black"),
    axis.text = element_text(size=13,face="plain",color="black"),
    legend.title=element_text(size=15,face="plain",color="black"),
    legend.text = element_text(size=14,face="plain",color="black")
  )

参考资料

《R语言数据可视化之美》——张杰

这是今天R可视化的学习笔记,我们下次再见。

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

本文分享自 庄闪闪的R语言手册 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 数据介绍
  • 散点复合饼图系列(a)
  • 散点复合饼图系列(b)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档