Shapefile issues in leaflet in R: Boundaries not showing for choropleth map

I am trying to make a choropleth map with imported shapefiles of districts.

I think my data is aligned with map because one district I was expecting lit up(but I am not sure). The problem is that I cannot see the shp file boundaries.

I used the code below:

districtsg <-readOGR("sample/copyfile/Districts/Map_of_Districts_.shp")

districtsg <- sp::spTransform(districtsg, CRS("+proj=longlat +datum=WGS84"))

districtsg <-subset(districtsg,is.element(districtsg$NAME,fb$districtlive))

fb <- fb [order(match(fb$districtlive,districtsg$NAME)),]



bins <- c(0,10,20,30,40,50)
pal<-colorBin("YlOrRd",domain=fb$per.content, bins=bins)


m<- leaflet() %>%
   setView(lng = -1.0232,lat=7.9465,zoom = 6) %>%
   addProviderTiles(providers$Stamen.Toner)%>%
   addPolygons(data =districtsg,
               weight = 1,
               smoothFactor = 0.5,
               color = "Blue",
               fillOpacity = 0.8,
               fillColor= ~pal(fb$per.content),
               highlight=highlightOptions(
                  weight = 5,
                  color = "#666666",
                  dashArray = "",
                  fillOpacity = 0.7,
                  bringToFront = TRUE
               ))
m

labels <- paste(  "<p>", fb$districtlive,"</p>",
                  "<p>", "% content:",round(fb$per.content,digits = 3),"</p>",
                  "<p>", "Total people that content:",round(fb$totalcontent,digits = 3),"</p>",
                  "<p>", "Total respondents from this district:",round(fb$totalrespondents,digits = 3),"</p>",sep = "" )



m<- leaflet() %>%
   setView(lng = -1.0232,lat=7.9465,zoom = 6) %>%
   addProviderTiles(providers$Stamen.Toner)%>%
   addPolygons(data =districtsg,
               weight = 1,
               smoothFactor = 0.5,
               color = "Blue",
               fillOpacity = 0.8,
               fillColor= ~pal(fb$per.content),
               highlight=highlightOptions(
                  weight = 5,
                  color = "#666666",
                  dashArray = "",
                  fillOpacity = 0.7,
                  bringToFront = TRUE
               ),
               label = lapply(labels,HTML)) %>%

     addLegend("topright",pal=pal, values = fb$per.content,
             opacity = 0.7,
              )
m

I looked around and found that the code below worked for someone, i tried it but it didn't work for me.

PRO <- sp::CRS('+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0')
DAT <- sp::spTransform(districtsg,PRO)

I am trying to make a choropleth map where districts boundaries can be seen and interacted with.

I used a sample data to do this and only one district was above 0. That district was the only that was coloured. I was expecting the other districts boundaries lines to present(and interactive) even if it had a 0% content and it was not coloured.

I did not get any error while doing this, it is just that, my map did look like what i was expecting.

I'd appreciate input on how to have my shape files on top of the tiles, with my data values present on top.

I suspect the issue is with this line

 districtsg <-subset(districtsg,is.element(districtsg$NAME,fb$districtlive))

You are subsetting your districts according to a condition that is not obvious from your code.

Try removing this line, and see what happens.

You might also consider moving from {rgdal} / {sp} based workflow to {sf} workflow = read the data in via districtsg <-sf::st_read("sample/copyfile/Districts/Map_of_Districts_.shp").

{sf} objects are modified data frames, and in general easier to troubleshoot than sui generis {sp} objects.

The spTransform line can be functionally replaced by districtsg <- sf::st_transform(districtsgh, 4326).

What this line does (and the previous in {sp} environment did) is that it transforms the coordinate reference system of your object from the current (unknown to us) to WGS84 which is in latitude & longitude and understood by leaflet.

The warning about palettes is not relevant to your problem of not showing borders, as it applies to fill of your polygons.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

I tried what you suggested using this code:

districtsg <-sf::st_read("sample/copyfile/Districts/Map_of_Districts_.shp")
districtsg <- sp::spTransform(districtsgh, CRS("+proj=longlat +datum=WGS84"))

and I got this error

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘spTransform’ for signature ‘"sf", "CRS"’

I removed the sf-transform code:

districtsg <- sp::spTransform(districtsgh, CRS("+proj=longlat +datum=WGS84"))

and run it and ended up with this warning:

Warning messages:
1: In pal(fb$per.content) :
  Some values were outside the color scale and will be treated as NA
2: sf layer is not long-lat data 

Do you help me understand why the first error messaged happend?