R 绘制渐变中国地图及添加南海九段图

采用中国国家基础地理信息系统(GIS)数据模板进行绘制并添加九段图。
调用R进行作图,所用中国地图模板可从[百度网盘](链接: https://pan.baidu.com/s/1FR2zG74d0aoOhfizT_BwHw )提取码: nefs下载使用

library("maptools")
library(rgdal)
library(ggplot2)
library(sp)
#fork 九段线
l9<-rgdal::readOGR("F:/statistics root/r workspace/Rstudio/SouthSea/九段线.shp")
#data reading
china_map=readShapePoly("F:/statistics root/r workspace/Rstudio/china/bou2_4p.shp")


ggplot(china_map,aes(x=long,y=lat,group=group)) +
  
  geom_polygon(fill="white",colour="grey") +
  
  coord_map("polyconic")
x <- china_map@data #读取行政信息

xs <- data.frame(x,id=seq(0:924)-1) #含岛屿共925个形状

china_map1 <- fortify(china_map) #转化为数据框

library(plyr)

china_map_data <- join(china_map1, xs, type = "full")
 #合并两个数据框
 
mydata <- read.csv("F:/statistics root/r workspace/Rstudio/china/2036english2.csv")
china_data <- join(china_map_data, mydata, type="full") 
china_data$ratio <- cut(china_data$ratio20361,breaks=c(1.20,1.25,1.30,1.35,1.40,1.45,1.50),labels=c('1.20~1.25','1.25~1.30','1.30~1.35','1.35~1.40','1.40~1.45','1.45~1.50'),right=FALSE,order=TRUE)
windowsFonts(myFont = windowsFont("Times New Roman"))
#rhg_cols1 <- c("#FEE0D2","#FCBBA1","#FC9272","#FB6A4A","#EF3B2C") #分等级并填充颜色
rhg_cols1 <- c("#fee0d2","#FCBBA1","#fc9272","#FB6A4A","#ef3b2c","#99000d") 
province_city <- read.csv("F:/statistics root/r workspace/Rstudio/china/chinaprovincecity2.csv") #省会坐标

p1 <- ggplot(china_data, aes(x = long, y = lat)) +
  
  geom_polygon(aes(group = group, fill = ratio),colour="grey30",size=0.05) +
  
  scale_fill_manual(values=rhg_cols1)+  
  
  guides(fill=guide_legend(title='Ratio of blood demand to supply'))+
  coord_map("polyconic") +
  geom_text(size=1.5,aes(x = jd,y = wd,label = province), hjust=0.5,vjust=0.5,data =province_city)+ #添加省会标签

  theme(                            #清除不必要的背景元素
    
    panel.grid = element_blank(),
    
    panel.background = element_blank(),
    
    axis.text = element_blank(),
    
    axis.ticks = element_blank(),
    
    axis.title = element_blank()
    
  )
p1  
  
 
#绘制小图
p2<-ggplot()+
  geom_polygon(data=china_data,aes(x=long,y=lat,group=group),color="grey40",fill="white")+ #绘制分省图
  geom_line(data=l9,aes(x=long,y=lat,group=group),color="red",size=0.5)+ #9段线
  coord_cartesian(xlim=c(105,125),ylim=c(3,30))+ #缩小显示范围在南部区域
  theme(
    aspect.ratio = 1.25, #调节长宽比
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    panel.background = element_blank(),
    panel.border = element_rect(fill=NA,color="grey20",linetype=1,size=0.8),
    plot.margin=unit(c(0,0,0,0),"mm"))
    
    
#嵌合2个图
library(grid) #ggplot也是grid图
vie <- viewport(width=0.15,height=0.10,x=0.68,y=0.25) #定义小图的绘图区域
p1 #绘制大图
print(p2,vp=vie) #在p1上按上述格式增加小图

  
  

作图效果如下
R 绘制渐变中国地图及添加南海九段图_第1张图片

参考

  1. 用ggplot2包绘制带南海诸岛的热力地图
  2. R 标准地图记(区分四川和重庆且有台湾和南海诸岛)

你可能感兴趣的:(R实践)