干货分享 | R-ggplo2 如何在空间图中绘制统计子图
0.问题导入
有时候我们会碰到这样一种情况,那就是空间图非常华丽,但很难定量地描述信息。于是,我们可能想着另附一副统计图说明问题。但是,强迫症爆发的时刻又特别想在左下角空白处填上统计子图,难搞哦?然后,问题来了……如何在空间图中绘制统计子图?今天这篇文章给出解决方案~
1.数据准备
main plot data (SSP(共享社会经济路径数据) 2010 GDP)
2.数据预处理
#1. import data
setwd('...input path...')
pl_df_m = read.csv('test.csv',header = T)
pl_df_m = pl_df_m[,-1]
pl_df_m = as.data.frame(pl_df_m)
pl_df_m$cuts = cut(pl_df_m$value, breaks = c(0,0.5,1,2,3,4,5,6,7,8,9,10,Inf))
3.划分区域分别统计GDP各区间大小出现的频率
注:实际操作过程中可以通过各大洲的边界裁剪选取数据,本例重点不在此,故采用经纬度范围进行分区统计
section1 = which(pl_df_m$long<(-100) & pl_df_m$long >=(-180))
section2 = which(pl_df_m$long<(0) & pl_df_m$long >=(-100))
section3 = which(pl_df_m$long<(100) & pl_df_m$long >=(0))
section4 = which(pl_df_m$long >=(100))
len_stat <- function(x){
sec1 = length(which(pl_df_m[section1,]$class ==x))
sec2 = length(which(pl_df_m[section2,]$class ==x))
sec3 = length(which(pl_df_m[section3,]$class ==x))
sec4 = length(which(pl_df_m[section4,]$class ==x))
return(c(sec1,sec2,sec3,sec4))
}
x = 1:12
s_trial = sapply(x,len_stat)
class_in_order = unique(as.character(cut(seq(0.2,11,0.5),breaks = c(0,0.5,1,2,3,4,5,6,7,8,9,10,Inf))))
# 完成子图统计数据表构建
pl_stat_df = data.frame(
section = rep(c('S1','S2','S3','S4'),
each = 12),
class = factor(rep(class_in_order,4)),
value = c(s_trial[1,],s_trial[2,],
s_trial[3,],s_trial[4,])
)
4.绘制主图
mycolor = colorRampPalette(brewer.pal(11,'Spectral'))(12)
color_group = paste0('a',1:12)
fontsize = 12
main_plot = ggplot()+
geom_hline(aes(yintercept = 50),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_hline(aes(yintercept = 0),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_hline(aes(yintercept = -50),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_vline(aes(xintercept = 0),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_vline(aes(xintercept = -100),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_vline(aes(xintercept = 100),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_tile(data = pl_df_m,aes(x = long,y = lat, fill = cuts))+
theme(panel.background = element_rect(fill = 'transparent',color = 'black'),
axis.text = element_text(face='bold',colour='black',size=fontsize,hjust=.5),
axis.title = element_text(face='bold',colour='black',size=fontsize,hjust=.5),
legend.position=c('bottom'),
legend.direction = c('horizontal'))+
scale_fill_manual(values = mycolor)+
coord_fixed(1.3)+
geom_hline(aes(yintercept = 0),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
guides(fill=guide_legend(nrow=2))+
xlab('Longitude')+
ylab('Latitude')
5.绘制子图
inset_plot = ggplot()+
geom_bar(data = pl_stat_df,aes(x = section, y = value,
fill= fct_inorder(class)),
stat = 'identity',position = 'fill')+
scale_fill_manual(values = mycolor)+
guides(fill=guide_legend(nrow=2))+
theme(legend.position = 'none')
6.将子图绘制进子图
integrated_plot = ggdraw()+
draw_plot(main_plot)+
draw_plot(inset_plot, x = 0.07, y = 0.37,
width = 0.22, height = 0.15)
7.图件导出为PNG 格式
png('test3.png',
height = 25,
width = 25,
units = 'cm',
res = 1000)
print(integrated_plot)
dev.off()
8.结果图示例
如图所示,完成在空间图左下角添加统计子图。
9.总结
本次绘图所用R-packages
注:如没有安装如下包的,请采用
install.pacakges('package name') 进行安装。
library(ggplot2)
library(cowplot)
library(data.table)
library(RColorBrewer)
library(forcats)
小编联系方式
作者:TroyShen
链接:https://www.jianshu.com/p/592b93e55a1d
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
图文编辑:张琦琦
责任编辑:郝娜
审核:王波涛 王冠
猜你喜欢
2019/12/14 Major Snow
都看到这里了,点个【在看】再走呗~