查看原文
其他

R可视乎|复合饼图系列

庄闪闪 庄闪闪的R语言手册 2022-06-23

散点复合饼图(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.1crime$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.00001y<-(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.00001ylabel<-seq(0,260,50)ybreak<-(ylabel-min(mydata$Robbery))/(max(mydata$Robbery)-min(mydata$Robbery))+0.00001mydata2<-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可视化的学习笔记,我们下次再见。


欢迎关注我的公众号,点赞,在看,收藏~~~


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

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