Search code examples
rleafletgeospatialprojectionr-sf

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


I am new to geo spatial data & trying to create choropleth map using leaflet & shapefile.

I have tried to plot the data on leaflet map but getting an error: Error: Must supply x/y attributes which seems to be due to some projection issues & not sure how to correct the projections in accordance with leaflet().

My shapefile Projections

ind_global$geometry %>% 
  st_crs()

output:

  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

(UPDATE:

Link to geometry dataset ind_global: https://github.com/johnsnow09/covid19-df_stack-code/blob/main/ind_global_rds.rds )

Code I tried:

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)

Also tried with below projections but getting error:

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

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


The Shape file when plotted using ggplot & geom_sf()

enter image description here


Solution

  • I think this issue at least when I ran your code is that Country.Region is a factor and not a character variable. I made just a few edits to your code and got what I think you are after:

    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)
    

    enter image description here