美文网首页R
【R】画中国地图的shp及其地理信息提取

【R】画中国地图的shp及其地理信息提取

作者: 南谛走心 | 来源:发表于2020-04-26 15:53 被阅读0次

    0. 地图数据信息

    教程合集 | 地图数据找不到?家园都有解决方案!

    1. 基本参考

    重要参考R地图系列(1):maptools包绘制中国地图,本链接解决了一个标准中国地区shp文件来源及其数据读取问题。

    2. 读nc文件用ggplot画图,把matrix、lon、lat转成一个data.frame

    #根据经纬度信息,把matrix变成对应经纬度的data.frame

    #lon lat val

    library(reshape2)

    colnames(dust_mass)<-lat

    rownames(dust_mass)<-lon

    dust_mass_df <- melt(dust_mass)

    colnames(dust_mass_df)<-c('lon','lat','val')

    3. 用ggplot画图

    (1) 简单画出来

    library(lattice)

    library(latticeExtra)

    library(viridisLite)

    library(ggplot2)

    library(raster)

    library(rasterVis)

    library(rgdal)

    library(grid)

    library(scales)

    library(viridis)  # better colors for everyone

    library(ggthemes) # theme_map()

    library(mapproj)

    library(sf)

    regionpath<-'E:/china-province-border-data-bou2_4p-shp/bou2_4p.shp'

    region<-readOGR(regionpath)

    getwd()

    dirname='E:/0423 Giovanni取MERRA2的沙尘数据判断沙尘源位置/'

    setwd(dirname)

    jpeg(file = "myplot3.jpeg",width=1200,height=800)

    #画图区

    ggplot() +

      geom_tile(data=dust_mass_df, aes(x=lon, y=lat, fill=val), alpha=0.8)+#网格

      geom_polygon(data=region, aes(x=long, y=lat, group=group),

                  fill="white",colour="black", size=0.5,alpha=0.1) +

      #geom_point(data=dust_mass_df, aes(x=lon, y=lat,colour = val),alpha=.9)+#站点

      labs(title=expression("MERRA2 2000-2019 monthly mean surface dust mass"))+

      #设置color

      #palette参考选择http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/figure/unnamed-chunk-14-1.png

      #scale_fill_viridis( name = "这是图例名称") +

      scale_fill_distiller(name=expression('Units: '*mu*"g /"*m^{3}*' ') ,palette="Spectral",limits=c(0,600),breaks=seq(0,600,100))+

      #scale_fill_gradient(name=expression('Units: "*mu*"g /"*m^{3}*' '),low = "blue", high = "red",limits=c(0,600),breaks=seq(0,600,100))+

      #添加rectangle

      #(1)参考An 2018的定义

      geom_rect(aes(xmin=76,xmax=90,ymin=37,ymax=42),linetype=2, alpha=0,size=1,color="black")+#R1

      geom_rect(aes(xmin=87,xmax=95,ymin=44,ymax=50),linetype=2, alpha=0,size=1,color="black")+#R2

      geom_rect(aes(xmin=97,xmax=110,ymin=38,ymax=45),linetype=2, alpha=0,size=1,color="black")+#R3

      geom_rect(aes(xmin=114,xmax=118,ymin=42,ymax=45),linetype=2, alpha=0,size=1,color="black")+#R3

      #theme_map() +

      theme(legend.position="bottom") + #"none", "left", "right", "bottom", "top", or two-element numeric vector

      theme(legend.key.width=unit(2, "cm"))+

      xlab("Longitude(°)")+

      ylab("Latitude(°)")+

      xlim(70,140)+ylim(15,55)+ #定义经纬度范围

      #设置投影方式

      coord_map("mercator")

      #coord_map("polyconic")

      #coord_map("lambert", lat0=0, lat=60)#Lambert,lat0,lat还是不太清楚啥意思

      #coord_map()

      #coord_equal() #+  #经纬度相同的画图方法

      #coord_fixed(ratio = 1)#+

    dev.off()

    相关文章

      网友评论

        本文标题:【R】画中国地图的shp及其地理信息提取

        本文链接:https://www.haomeiwen.com/subject/ziqjwhtx.html