R-ggplo2-如何在空间图中绘制统计子图
2019-12-03 本文已影响0人
TroyShen
0. 问题导入
有时候我们会碰到这样一种情况,那就是空间图非常华丽,但很难定量地描述信息。于是,我们可能想着另附一副统计图说明问题。但是,强迫症爆发的时刻又特别想在左下角空白处填上统计子图,难搞哦(图1)?然后,问题来了。。。如何在空间图中绘制统计子图?今天这篇文章给出解决方案~
绘图所用packages 附于文末
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. 结果图示例
如图2所是,完成在空间图左下角添加统计子图。
图2
8. 总结
本次绘图所用R-packages
注:如没有安装如下包的,请采用install.pacakges('package name')进行安装
library(ggplot2)
library(cowplot)
library(data.table)
library(RColorBrewer)
library(forcats)