0
我正在学习如何使用R和Leaflet。我几乎完成了使用加利福尼亚县的地图,但我不喜欢我可以在地图上看到其他州。我想把我的多边形(县)周围的地图画出来。我看到类似的问题在其他地方解决,但我不知道如何在代码中应用所述内容。有人可以检查这个代码,并建议我需要添加什么?将Leaflet视图的可视部分限制为多边形区域
如果你滚动到这个链接的底部,你会看到我想要做的。 http://rpubs.com/stefanya/127436
我使用的代码是:
#loading shapefile
counties <- readOGR("./shapefiles", layer="cb_2014_us_county_20m")
#filtering for only california
counties <- subset(counties, [email protected]$STATEFP=="06")
#making a leaflet map of california counties
leaflet() %>% addTiles() %>% addPolygons(data=counties)
#merging the data into this shapefile
[email protected] = data.frame([email protected], sumByCounty[match([email protected][,"NAME"], sumByCounty[,"NAME"]),])
#set color palette
colorRamp <- colorRamp(c("#2c7fb8","#7fcdbb","#edf8b1"), interpolate = "spline")
palette <- colorNumeric(colorRamp, [email protected]$progress)
leaflet() %>% addProviderTiles("Stamen.TonerLite") %>%
addPolygons(
weight= 2,
stroke = TRUE,
fillOpacity = .65,
data=counties,
color = ~palette(progress),
popup = ~paste("<strong>County:</strong>",NAME,
"<br>",
"<strong>Total Responses:</strong>",sumByCounty,
"<br>",
"<strong>Complete:</strong>",progress,"<strong>%</strong>")
) %>% addLegend(title = "Response <br> Goal Met", pal = palette, values = [email protected]$progress, bins=5, opacity = 1, position="topright", labFormat = labelFormat(suffix = '%'))
一个简单的解决办法是建立在其他国家一大多边形来隐藏他们... – ghybs
你也许可以组成你自己的自定义瓷砖。 https://help.openstreetmap.org/questions/136/how-do-i-render-my-own-maps-for-my-website –
这是链接到“类似的问题解决其他地方”:http:///gis.stackexchange.com/questions/73048/how-do-i-clip-osm-basemap-with-a-polygon,因为它基本上是@ghybs解决方法,并且可以很容易地应用于传单。 – Spacedman