0. 問題導入
有時候我們會碰到這樣一種情況,那就是空間圖非常華麗叛本,但很難定量地描述信息沪蓬。于是,我們可能想著另附一副統(tǒng)計圖說明問題来候。但是跷叉,強迫癥爆發(fā)的時刻又特別想在左下角空白處填上統(tǒng)計子圖,難搞哦(圖1)营搅?然后云挟,問題來了。转质。园欣。如何在空間圖中繪制統(tǒng)計子圖?今天這篇文章給出解決方案~
繪圖所用packages 附于文末
1. 數(shù)據(jù)準備
main plot data (SSP(共享社會經(jīng)濟路徑數(shù)據(jù)) 2010 GDP)
2. 數(shù)據(jù)預處理
#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. 劃分區(qū)域分別統(tǒng)計GDP各區(qū)間大小出現(xiàn)的頻率
注:實際操作過程中可以通過各大洲的邊界裁剪選取數(shù)據(jù)峭拘,本例重點不在此俊庇,故采用經(jīng)緯度范圍進行分區(qū)統(tǒng)計
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))))
# 完成子圖統(tǒng)計數(shù)據(jù)表構建
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所是,完成在空間圖左下角添加統(tǒng)計子圖鸡挠。
8. 總結
本次繪圖所用R-packages
注:如沒有安裝如下包的辉饱,請采用install.pacakges('package name')進行安裝
library(ggplot2)
library(cowplot)
library(data.table)
library(RColorBrewer)
library(forcats)