将州边界添加到传单的输出中,其中输入是县 shapefile

add state borders to the output of leaflet where the input is counties shapefile

我从 data.gov 下载了县 shapefile。我可以在传单中使用它们来绘制 WA、ID 和 OR 中的县。我想让州边界变粗。我怎样才能做到这一点?

counties <- readOGR(paste0(dir, "/tl_2017_us_county.shp"),
                    layer = "tl_2017_us_county", GDAL1_integer64_policy = TRUE)

counties <- counties[counties@data$STATEFP %in% c("16", "41", "53"), ]
counties <- subset(counties, STATEFP %in% c("16", "41", "53") )
counties <- rmapshaper::ms_simplify(counties)

counties %>%
leaflet() %>%
setView(lng = -118.4942, lat = 47.2149, zoom = 5) %>%
addPolygons( fillColor = "green", fillOpacity = 0.5,
             color = "black", opacity = 1.0, weight = .6, smoothFactor = 0.5,
             highlightOptions = highlightOptions(color="white", weight=2, bringToFront = TRUE),
             label= ~ NAME) 

P.S. 我想在 R studio 中做这个,我不知道这里的任何高级编码:https://leafletjs.com/


编辑 目标是使状态可区分,我用以下方法做到了:

factpal <- colorFactor(topo.colors(5), counties$category)

counties %>%
leaflet() %>%
# addTiles() %>% 
setView(lng = -118.4942, lat = 47.2149, zoom = 5) %>%
addPolygons( fillColor = ~factpal(STATEFP), fillOpacity = 0.5,
             # The following line is associated with borders
             color = "black", opacity = 1.0, weight = .6, smoothFactor = 0.5,
             highlightOptions = highlightOptions(color="white", weight=2, bringToFront = TRUE),
             label= ~ NAME)

我还是很想知道加粗边框的答案

可能没有 in-built 选项来实现此目的,因为您正在尝试绘制属于一个州的所有 county-polygons 的轮廓。当您通过为其中的所有多边形着色来突出显示状态时,它当然有效。

要使用 sp-package 实现您想要的效果,您可以这样做:

library(sp)
library(leaflet)

states <- aggregate(counties[, "STATEFP"], by = list(ID = counties@data$STATEFP), 
                    FUN = unique, dissolve = T)

counties %>%
  leaflet() %>%
  setView(lng = -118.4942, lat = 47.2149, zoom = 5) %>%
  addPolygons( fillColor = "green", fillOpacity = 0.5,
               color = "black", opacity = 1.0, weight = .6, smoothFactor = 0.5,
               highlightOptions = highlightOptions(color="white", weight=2, bringToFront = TRUE),
               label= ~ NAME) %>%
  addPolylines(data = states, color = "black", opacity = 1, weight = 3)