如何在R's Leaflet中的“addPolygons”之上添加“addTiles”?

时间:2017-05-09 22:47:16

标签: r leaflet

如何在R的宣传单包中设置图层顺序,以便图块显示在填充颜色的多边形之上?

这是我到目前为止所得到的:

require(leaflet)
require(acs)
require(tigris)
require(rgdal)
census.income.end.year = 2015
county = 17
nd.counties=acs.fetch(geography=geo.make(state="ND", county=county), 
                      table.number="B01003", endyear = 2015)

tracts <- tigris::tracts(state = 'ND', county = county, cb=FALSE, year = 2015)

# create a geographic set to grab tabular data (acs)
geo<-geo.make(state=c("ND"),
              county = county,
              tract="*")

# add in median income
median.income <- acs.fetch(endyear = census.income.end.year, 
                           geography = geo, 
                           variable = c("B19013_001"))
income_df <- data.frame(paste0(as.character(median.income@geography$state), 
                               str_pad(as.character(median.income@geography$county), 3, 'left', '0'), 
                               str_pad(as.character(median.income@geography$tract), 5, 'left', '0')), 
                        median.income@estimate)

rownames(income_df)<-1:nrow(income_df)
names(income_df)<-c("GEOID", "hhincome")
income_merged <- geo_join(tracts, income_df, "GEOID", "GEOID")

income_merged <- spTransform(income_merged, CRS("+init=epsg:4326"))

qpal <- colorQuantile("plasma", income_df$hhincome, n = 4)

leaflet() %>% 
  setView( -96.7898, 46.8772, zoom=11) %>% 
  addPolygons(data = income_merged, 
              fillColor = qpal(income_merged$hhincome), 
              fillOpacity = 1, 
              weight = 0.3) %>%
  addProviderTiles(providers$Hydda.RoadsAndLabels) 

最终,我喜欢使用自定义MapBox使用addTiles(而不是上面的代码中的addProviderTiles)来做这个,但是我无法弄清楚如何使这个例子具有可重现性......鉴于你需要一个键才能访问自定义MapBox图块(顺便说一句,我创建了一个自定义的MapBox图块,除了道路和标签之外应该是透明的,因此底层多边形应该“显示”。)

这是使用非R版本的传单在{+ 3}}上添加磁贴在圆圈顶部的一种方法,但我不知道如何在我的上下文中使用该工作。我认为添加“窗格”可能是要走的路,但我没有看到R传单中的功能。此外,我探索了z-index值,但这似乎是一个死胡同。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:5)

R传单现在包含addMapPane功能。解决此问题的方法是先设置窗格顺序,然后添加平铺/多边形。可复制的示例:

library(leaflet)
library(geojsonio)

# get polygon data
# https://github.com/simonepri/geo-maps/blob/master/info/countries-land.md
world <- geojson_read(
  "https://github.com/simonepri/geo-maps/releases/download/v0.6.0/countries-land-10km.geo.json", 
  what = "sp"
)

# generate random values
world@data$value <- runif(nrow(world@data))

# get color palette
color_pal <- colorNumeric(palette = "YlOrRd", domain = NULL)

# get leaflet map
leaflet() %>%
  setView(lat = 50, lng = 15, zoom = 4) %>%
  addMapPane("background_map", zIndex = 410) %>%  # Level 1: bottom
  addMapPane("polygons", zIndex = 420) %>%        # Level 2: middle
  addMapPane("labels", zIndex = 430) %>%          # Level 3: top
  addProviderTiles(
    providers$Esri.WorldTerrain,
    options = pathOptions(pane = "background_map")
  ) %>%
  addPolygons(
    data = world, stroke = FALSE, smoothFactor = 0.2,
    fillOpacity = 0.6, fillColor = ~color_pal(value),
    options = pathOptions(pane = "polygons")
  ) %>%
  addProviderTiles(
    providers$Stamen.TonerLabels,
    options = pathOptions(pane = "labels")
  )