gpt4 book ai didi

r - 如何在 R 的传单中的 "addTiles"之上 "addPolygons"?

转载 作者:行者123 更新时间:2023-12-04 08:03:46 25 4
gpt4 key购买 nike

您如何在 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)来执行此操作,但我无法弄清楚如何使该示例可重现...假设您需要一个 key 访问自定义 MapBox 瓦片(顺便说一句,我创建了一个自定义 MapBox 瓦片,除了道路和标签之外应该是透明的,因此底层多边形应该“显示”。)

这是使用非 R 版本的传单在圆圈顶部添加图 block 的一种方法: http://jsfiddle.net/dcu9pz2w/ ,但我不知道如何在我的上下文中使其工作。我认为添加“ Pane ”可能是要走的路,但我在 R 传单中看不到该功能。另外,我探索了 z-index 值,但这似乎是一个死胡同。

任何帮助深表感谢!

最佳答案

R 传单现在包括 addMapPane 功能。此问题的解决方案是首先设置 Pane 顺序,然后添加图 block /多边形。可重现的例子:

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")
)

关于r - 如何在 R 的传单中的 "addTiles"之上 "addPolygons"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43881007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com