单张setview()查看县地图

时间:2018-09-26 20:59:47

标签: r leaflet

你好,所有专家,

我对R和传单很陌生

我想创建一个闪亮的仪表板,并在其中创建带有传单的地图。这就是我想要的样子,首先,用户将选择一个州,然后,您可能会或可能不会选择一个县。因此,如果他们选择一个州,我想使用setview()来查看所选的州,如果他们选择一个州和一个县,则可以使用setview()来查看所选的县。

所以首先,我想确保我的传单代码先工作,然后再将其嵌入闪亮的服务器中。因此,假设用户选择了加利福尼亚州和洛杉矶县,我运行了以下代码。

我遇到以下错误,不确定如何解决。

有什么建议吗?提前非常感谢您!

Error in cut.default(x, binsToUse, labels = FALSE, include.lowest = TRUE,  : 
  'breaks' are not unique

> head(lonlat)
  STATE            COUNTY         county.State       lon      lat
1    AK   KENAI PENINSULA   KENAI PENINSULA,AK -151.3044 60.58293
2    AK         ANCHORAGE         ANCHORAGE,AK -149.8557 61.22002
3    AK            DENALI            DENALI,AK -145.8662 63.04024
4    AK            BETHEL            BETHEL,AK -161.7558 60.79222
5    AK MATANUSKA-SUSITNA MATANUSKA-SUSITNA,AK -150.5125 61.54361
6    AK     KODIAK ISLAND     KODIAK ISLAND,AK -152.3539 57.80459
> 



states <- readOGR("cb_2015_us_state_20m.shp",
                  layer = "cb_2015_us_state_20m", GDAL1_integer64_policy = TRUE)

states <- subset(states, states$STUSPS %in% c("CA"))
class(states)

leaflet(data = map_dat[map_dat$STATE %in% "CA" &
                         map_dat$COUNTY %in% "LOS ANGELES",]) %>%
  addProviderTiles(
    providers$Stamen.TonerLite,
    options = providerTileOptions(noWrap = TRUE)

  ) %>%
  addPolygons(data=states,color = "#444444", weight = 1, smoothFactor = 0.5,
              opacity = 1.0, fillOpacity = 0.5,
              fillColor = ~colorQuantile("YlOrRd", ALAND)(ALAND),
              highlightOptions = highlightOptions(color = "white", weight = 2,
                                                  bringToFront = TRUE)) 

selectedCounty <- lonlat[lonlat$COUNTY == "LOS ANGELES" &&
                           lonlat$STATE == "CA", ]
#leafletProxy(mapId = "allmap") %>%
setView(lng = selectedCounty$lon, lat = selectedCounty$lat, zoom = 5)

0 个答案:

没有答案