I downloaded the county shapefile
off of data.gov. I can use them in leaflet as follows to plot counties in WA, ID and OR. I would like to make the state borders thicker. How can I do that?
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. I would like to do this in R studio, I do not know any of the advanced codings in here: https://leafletjs.com/
Edit The goal was to make the states to be distinguishable, which I did with the following:
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)
I still would like to know the answer of making borders thicker.
There is probably no in-built option to achieve this, since you're trying to plot the outline of all county-polygons belonging to a state. It works of course when you highlight the states by coloring all polygons inside it.
To achieve what you want with the sp
-package you can do this:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With