如何在 r 中找到传单地图的 shapefile 数据的正确投影?

How to find correct projection of the shapefile data for leaflet map in r?

我是 geo spatial 数据的新手,正在尝试使用 leaflet 创建 choropleth 地图 & shapefile.

我试图在 传单地图 上绘制数据,但出现错误:错误:必须提供 x/y attributes 这似乎是由于一些 投影问题 并且不确定如何根据 leaflet() 更正投影。

我的 shapefile 投影

ind_global$geometry %>% 
  st_crs()

输出:

  User input: WGS 84 
  wkt:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["latitude",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["longitude",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
library(raster)

crs(ind_global$geometry)

CRS arguments: +proj=longlat +datum=WGS84 +no_defs

(更新:

Link 到几何数据集 ind_global: https://github.com/johnsnow09/covid19-df_stack-code/blob/main/ind_global_rds.rds )

我试过的代码:

library(tidyverse)
library(sf)
libraary(leaflet)
library(htmlwidgets)

pal <- colorBin(palette = "OrRd", 9, domain = ind_global$total_vaccinations)
ind_global %>% 
  st_as_sf() %>% 
  st_transform(crs = "+init=epsg:4326") %>% 
  leaflet() %>% 
  addProviderTiles(provider = "CartoDB.Positron") %>% 
  add_polygons(label = Country.Region,
               stroke = FALSE,
               smoothFactor = .5,
               opacity = 1,
               fillOpacity = 0.7,
               fillColor = ~ pal(total_vaccinations),
               highlightOptions = highlightOptions(weight = 5,
                                                   fillOpacity = 1,
                                                   bringToFront = TRUE)) %>% 
  
  addLegend("bottomright",
            pal = pal,
            values = ~ total_vaccinations,
            title = "total Vaaccinations",
            opacity = 0.7)

也尝试了以下预测但出现错误:

st_transform(crs = "+proj=longlat +datum=WGS84 +no_defs")

st_transform(crs = "+proj=longlat +ellps=GRS80")


使用 ggplot & geom_sf() 绘制时的 Shape 文件

我认为这个问题至少在我 运行 你的代码是 Country.Region 是一个因素而不是一个字符变量。我对您的代码进行了一些编辑,得到了我认为您想要的结果:

library(sf)
library(leaflet)
library(htmlwidgets)
library(riskyr)

ind_global<-readRDS("C:/Users/SCMCKENZIE/Downloads/ind_global_rds.rds")

pal <- colorBin(palette = "OrRd", 9, domain = ind_global$total_vaccinations)

tmp<-ind_global %>% 
  st_as_sf() %>% 
  st_transform(crs = "+init=epsg:4326")
tmp %>% leaflet() %>% 
  addProviderTiles(provider = "CartoDB.Positron") %>% 
  addPolygons(label = as.character(tmp$Country.Region),
               stroke = FALSE,
               smoothFactor = .5,
               opacity = 1,
               fillOpacity = 0.7,
               fillColor = ~ pal(total_vaccinations),
               highlightOptions = highlightOptions(weight = 5,
                                                   fillOpacity = 1,
                                                   bringToFront = TRUE)) %>% 
  
  addLegend("bottomright",
            pal = pal,
            values = ~ total_vaccinations,
            title = "total Vaaccinations",
            opacity = 0.7)