使用R Leaflet放大到State以查看ZipCode [英] Zooming into State to view ZipCode using R Leaflet

查看:86
本文介绍了使用R Leaflet放大到State以查看ZipCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R leaftlet程序包来创建美国的交互式Choropleth.

I am using R leaftlet package to create a interactive choropleth of the U.S.

有几本在线教程,我能够通过弹出窗口和缩放来创建交互式状态级别地图.另外,我还能够再次使用弹出窗口创建一个单独的邮政编码级别映射.

There are several tutorials online and I am able to create interactive state level map with popups and zooming. Also I was also able to create a separate zip code level map again with popups.

我希望在一个地图中同时显示两个视图,但是在缩放状态或双击状态时使邮政编码可见.就像如果我双击纽约",则将打开纽约邮政编码". R中有一个软件包/功能可以帮助我做到这一点吗?

I would like both views in one map itself but make zip code visible as I zoom in a state or double click on a state. Like If I double click on New York, the New York Zip Code opens up. Is there a package/function in R that can help me do this?

下面是这两者的静态屏幕截图,目的是为了使我可以清楚地计划要集成的内容.

Here are static screenshots of both to make things clear what I plan to integrate.

推荐答案

我同意Yehoshapat Schellekens的观点,即R中可能没有Web编程语言的灵活性.但是很少R不够灵活,无法获得理想的结果! :)在这里,您将获得基本需要的原始"示例.您可以使用一些JS自定义Windows弹出窗口.

I agree with Yehoshapat Schellekens that in R one might not have the flexibility of a web programming language. But seldom R is not flexible enough to achieve fancy results! :) Here you go with a "vanilla" example of what you basically need. You can customize the windows popup with some JS.

library(shiny)
library(leaflet)
library(maps)
library(maptools)
library(sp)
library(rgeos)

mapStates = map("state", fill = TRUE, plot = FALSE)
mapCounty = map("county", fill = TRUE, plot = FALSE)

shinyApp(
  ui = fluidPage(leafletOutput('myMap'),
             br(),
             leafletOutput('myMap2')),

  server <- function(input, output, session) {
      output$myMap <- renderLeaflet({
        leaflet() %>%
          addProviderTiles("Stamen.TonerLite",
                           options = providerTileOptions(noWrap = TRUE)) %>%
          addPolygons(lng = mapStates$x, 
                      lat = mapStates$y, 
                      fillColor = topo.colors(10, alpha = NULL), 
                      stroke = FALSE)
  })

  observeEvent(input$myMap_shape_click, {
        click <- input$myMap_shape_click
        if(is.null(click))
          return()       

    lat <- click$lat
    lon <- click$lng

    coords <- as.data.frame(cbind(lon, lat))
    point <- SpatialPoints(coords)
    mapStates_sp <- map2SpatialPolygons(mapStates, IDs = mapStates$names)
    i <- point [mapStates_sp, ]
    selected <- mapStates_sp [i]
    mapCounty_sp <- map2SpatialPolygons(mapCounty, IDs = mapCounty$names)
    z <- over(mapCounty_sp, selected)
    r <- mapCounty_sp[(!is.na(z))] 

    output$myMap2 <- renderLeaflet({
        leaflet() %>% 
          addProviderTiles("Stamen.TonerLite",
                       options = providerTileOptions(noWrap = TRUE)) %>%
          addPolygons(data=r,
                      fillColor = topo.colors(10, alpha = NULL), 
                      stroke = FALSE)
    })  
  })
})

注意:示例中使用的数据集似乎具有不同的精度(对于州和县来说不是完美的重叠).因此,空间匹配所占的县比预期的要多(内部的那些以及与州边界相交的那些县).请使用名称作为ID,以实现完美匹配.

NOTE: The datasets used in the example seem to have different accuracies (not perfect overlap for states and counties). Therefore the spatial matching is accounting for more counties than expected (those inside plus those intersecting the state borders). Use the name as ID instead to achive the perfect match.

这篇关于使用R Leaflet放大到State以查看ZipCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆