【问题标题】:Map Arctic/subarctic regions in ggplot2 with Lambert Conformal Conic projection in R使用 R 中的 Lambert Conformal Conic 投影在 ggplot2 中映射北极/亚北极地区
【发布时间】:2019-11-18 16:33:57
【问题描述】:

我正在尝试使用 ggplot2 绘制北极/亚北极地区的纬度/经度位置,并按类型为它们着色。

这是我正在使用的包:

library(ggplot2)
library(rgdal)
library(ggmap)
library(sp)
library(dplyr) 
library(ggspatial) #To use geom_sf to add shapefiles

这是我的数据示例:

dat <- data.frame(
  "Lat" =  c(70.5,74.5,58.5,60.5), 
  "Lon" = c(-21.5,19.0,-161.5,-147.5), 
  "Type"=c("A","B","A","B")
)
dat

我为北极圈创建了一个 shapefile,可在此处找到:https://www.arcgis.com/home/item.html?id=f710b74427a14a1d804e90fbf94baed4

ArcticCircle <- sf::st_read("C:/.../LCC_AC.shp")

我正在尝试使用 ggplot2 进行映射,但我找不到使用 Lambert Conformal Conic 投影添加底图的方法。

我知道您可以使用 coord_sf() 来指定投影和边界,但我找不到圆锥投影的代码。

p <- ggplot()+
geom_point(data = dat, aes(x = Lon, y = Lat, colour = Type))+
geom_sf(data = ArcticCircle, linetype = "dashed", aes())+
xlab("Longitude")+
ylab("Latitude")+
p

我的地图边界最好是在大约 45 度纬度的北极圈周围的一个圆圈。如果无法制作圆形边界,那么围绕该纬度的矩形也可以。

我对 R 比较陌生,因此我们将不胜感激!

【问题讨论】:

    标签: r ggplot2 projection ggmap ggspatial


    【解决方案1】:

    这可能会有所帮助:

    国界数据

    library("rnaturalearth")
    library("rnaturalearthdata")
    world <- ne_countries(scale = "medium", returnclass = "sf")
    

    然后可以裁剪和绘制感兴趣的区域如下:

    world_cropped <- st_crop(world, xmin = -180.0, xmax = 180.0,
                              ymin = 45.0, ymax = 90.0)
    ggplot(data = world_cropped) + 
      geom_sf() + 
      geom_sf(data = ArcticCircle, linetype = "dashed", aes())+
      geom_sf(data = dat_sf, color = 'red') + 
      coord_sf(crs = 
                 "+proj=lcc +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0") 
    

    【讨论】:

      【解决方案2】:

      我在您的代码中发现了一些错误,首先,您的 dat 数据框包含字符串格式的 x 和 y 值,并且不是数字(这在绘图时没有帮助!)。

      其次,与其他 GIS 软件不同,R 不做 On the fly 投影转换!因此,将您的点与 LAT LONG 一起使用对您的 shapefile 不起作用,因为它在不同的 CRS 中!这是北极圈的 CRS:

      proj4string:    +proj=lcc +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs
      

      所以,我所做的是将你的 LAT LONG 点文件转换为上面显示的 CRS,然后制作 ggplot,我将所有代码放在下面,用 cmets:

      library(ggplot2)
      library(rgdal)
      library(ggmap)
      library(sp)
      library(dplyr) 
      library(ggspatial) #To use geom_sf to add shapefiles
      
      #### Breaking apart all the values
      x = c(-21.5,19.0,-161.5,-147.5)
      y = c(70.5,74.5,58.5,60.5)
      Type =c("A","B","A","B")
      
      ### Creating spatial LAT LONG coordinates, which will be converted to Lambert Conformal Conic Projection below
      dat <- data.frame(lon = x, lat = y)
      
      #### Creating LAT LONG SpatialPoints
        coordinates(dat) = c("lon", "lat")
      proj4string(dat) <- CRS("+init=epsg:4326")
      
      #### The coordinate reference system, that is used in your shapefile. Will use this when converting the spatial points
      polar = "+proj=lcc +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"
      
      ### Converting the LAT LONG to the polar CRS shown above
      polar_dat = spTransform(dat, polar)
      polar_dat = as.data.frame(polar_dat)
      
      #### Adding the Type column back to the data frame, with the new polar coordinates
      polar_dat = data.frame(polar_dat, Type)
      
      #### Reading in the Circle Shapefile
      ArcticCircle = st_read("P:\\SHP\\LCC_AC\\LCC_AC.shp")
      
      ### Putting it togather in ggplot
      p <- ggplot()+
        geom_point(data = polar_dat, aes(x = lon, y = lat, colour = Type))+
        geom_sf(data = ArcticCircle, linetype = "dashed", aes())+
        xlab("Longitude")+
        ylab("Latitude")
      

      剧情最后是这样的:

      希望对您有所帮助,如果有任何不清楚的地方请告诉我!

      编辑:带有底图的新代码(感谢 Majid 提供数据)

      library(ggplot2)
      library(rgdal)
      library(ggmap)
      library(sp)
      library(dplyr) 
      library(ggspatial)
      library(sf)
      library(rnaturalearth)
      library(rnaturalearthdata)
      
      #### Breaking apart all the values
      x = c(-21.5,19.0,-161.5,-147.5)
      y = c(70.5,74.5,58.5,60.5)
      Type =c("A","B","A","B")
      
      ### Creating spatial LAT LONG coordinates, which will be converted to Lambert Conformal Conic Projection below
      dat <- data.frame(lon = x, lat = y)
      
      #### Creating LAT LONG SpatialPoints
      coordinates(dat) = c("lon", "lat")
      proj4string(dat) <- CRS("+init=epsg:4326")
      
      #### The coordinate reference system, that is used in your shapefile. Will use this when converting the spatial points
      polar = "+proj=lcc +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"
      b <- bbox(dat)
      
      ### Converting the LAT LONG to the polar CRS shown above
      polar_dat = spTransform(dat, polar)
      polar_dat = as.data.frame(polar_dat)
      
      #### Adding the Type column back to the data frame, with the new polar coordinates
      polar_dat = data.frame(polar_dat, Type)
      
      #### Reading in the Circle Shapefile
      ArcticCircle = st_read("P:\\SHP\\LCC_AC\\LCC_AC.shp")
      
      ### Getting basemap shapefile
      world <- ne_countries(scale = "medium", returnclass = "sf")
      world_cropped <- st_crop(world, xmin = -180.0, xmax = 180.0,
                               ymin = 45.0, ymax = 90.0)
      
      ### Plotting it all togather
      p = ggplot(data = world_cropped) + 
        geom_sf(colour = "#6380ad", fill = "#9cb3db") + 
        geom_sf(data = ArcticCircle, linetype = "dashed", aes())+
        geom_point(data = polar_dat, aes(x = lon, y = lat, colour = Type))+
        coord_sf(crs = 
                   "+proj=lcc +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0")
      

      【讨论】:

      • 谢谢!糟糕,抱歉,编辑了问题以删除数据框中数字周围的引号!我的实际数据框是从 .csv 导入的,所以我没有这个问题。我使用我的实际数据运行了这段代码,它奏效了!是否可以添加北极国家的底图?
      • 很高兴它工作:),可以添加底图(使用:ggmap(get_map(location = bbox(YOUR SHAPEFILE HERE TO GET EXTENT))),但不幸的是我认为你不能添加除 WGS 1984 投影以外的底图,该底图仅适用于 LAT LONG,不适用于上面的地图。如果您手动添加另一个带有北极国家/地区的 shapefile,还有另一种选择。 (但要确保 CRS 对所有人都是一样的)
      • 所以我注意到在你上面的情节中,北极圈在顶部被切断了。这也发生在我的身上,我不确定为什么。就底图而言,我还没有在网上找到shapefile,但我正在努力!
      • 有人发布了上面的国家数据。另外,北极圈顶部被切断的原因也是因为shapefile就是这样!
      • 我实际上已经编辑了我的答案并发布了一个包含底图的新代码
      猜你喜欢
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 2016-08-07
      • 2018-11-20
      • 2012-05-21
      • 2020-08-29
      相关资源
      最近更新 更多