1

I have created a map where i have plotted population density with a shapefile using the Leaflet package. I'am trying to loop through each polygon on the map, is this possible and how could do it?

**Map: Leaflet map

#Create Leaflet map
map <- leaflet()  %>% addTiles() %>% 
  setView(lng = -7.1050, lat=53.2000,zoom=8) %>% 

  addPolygons(data=m,weight=1, popup = map_popup2, fillOpacity = .5) %>% 

  addCircleMarkers(data = m, lng = ~long_xcord, 
                   lat = ~lat_ycord, radius=.5, color="green") %>%

  addCircles(data = m, lng = ~long_xcord, 
             lat = ~lat_ycord, popup = map_popup,
             radius = 4828.03, fillOpacity = 0.1, #3218.69 = 2 miles
             color = 'black', fillColor = 'blue',weight = 2, label=m$` name`) %>%

  addLegend("bottomright", pal = heat, values = m$pop,
            title = "Population",
            opacity = 1)
1
  • Please water down your code. It's not entirely clear why all this is necessary given that we aren't able to reproduce sans your data. Commented Apr 2, 2017 at 12:39

1 Answer 1

2

Just use [. Here's a modified example from ?"SpatialPolygonsDataFrame-class"

library(sp)

Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)

Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)

plot(0,0, xlim = c(0, 10), ylim = c(0, 5), type = "n")

for (i in 1:nrow(coordinates(SpP))) {
  plot(SpP[i, ], add = TRUE)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.