R,Leaflet多边形添加黑色边框

时间:2016-07-08 06:51:52

标签: r shiny leaflet border polygon

我想在我的多边形周围添加黑色边框。请参阅下面的图片到目前为止。

enter image description here

我使用了这段代码。我不知道如何包含一个zip文件,所以它可以重现。

library("rgdal")
library("leaflet")

 amsterdam.districts <- shapefile("~/R/Shiny/Shapefiles/sd2010zw_region.shp")
 amsterdam.districts <- spTransform(amsterdam.districts, CRS("+init=epsg:4326"))
 amsterdam.districts$STADSDEELN<- c("Centrum","Westpoort","West","Nieuw West","Zuid","Oost","Noord","Zuidoost")

 amsterdam.districts$SDNUMMER=as.factor(amsterdam.districts$SDNUMMER) 
 factpal=colorFactor(brewer.pal(n = 11, name ="Spectral") , amsterdam.districts$SDNUMMER) 

   leaflet() %>%
     addPolygons(
       data =amsterdam.districts, 
       stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5,
       color = "grey",
       fillColor = ~factpal(SDNUMMER),
       layerId = amsterdam.districts@data$STADSDEELN)%>%
     addProviderTiles("CartoDB.Positron", options= providerTileOptions(opacity = 1))

感谢阅读!

马亭

1 个答案:

答案 0 :(得分:11)

这是一个可重复的例子:

library(mapview)
library(RColorBrewer)

factpal <- colorFactor(brewer.pal(n = 11, name ="Spectral") , gadmCHE$NAME_1) 

leaflet() %>%
  addPolygons(
    data = gadmCHE, 
    stroke = TRUE, fillOpacity = 0.5, smoothFactor = 0.5,
    color = "black", opacity = 1,
    fillColor = ~factpal(gadmCHE$NAME_1)) %>%
  addProviderTiles("CartoDB.Positron", options = providerTileOptions(opacity = 1))

您只需设置stroke = TRUEcolor = "black"即可获取边框。请注意,我还设置opacity = 1以防止半透明边框。