我创建了一个密度贴图,但是R选择的容器尺寸太大。我要创建自己的纸槽尺寸。 https://imgur.com/mR1L7be
该图像显示bin的大小为完整整数,不会产生任何有意义的结果。我也无法显示Esri.WorldGrayCanvas基本地图,但这并不重要。
library(tmap)
library(tmaptools)
library(leaflet)
library(tidyverse)
us_geo = read_shape("taxi_zones_sp.shp",as.sf = T,stringsAsFactors = F)
popmap = append_data(us_geo
,dropoffs
,key.shp = "Taxi_zone"
,key.data = "pu_taxi_zone")
#turn na's into zero.
popmap = popmap %>%
mutate_all(funs(replace(., is.na(.), 0)))
my_map =
tm_shape(popmap) +
tm_fill("perc", palette = "Oranges"
,title = "pickup density (%)", id = "Taxi_zone") +
tm_borders(alpha=.4)
leaflet() %>%
setView(lng = -74.058913, lat = 40.689852, zoom = 10) %>%
addProviderTiles("Esri.WorldGrayCanvas") + my_map
我希望0显示为白色,其余显示为.5增量。 并在可能的情况下在我的地图上显示Esri.WorldGrayCanvas。 我也想将地图保持为静态格式。因此,缩放等图标/控件并不理想。
答案 0 :(得分:1)
我无权访问您的数据集,因此我刚刚从NYU下载了外观相似的数据。为了正确设置垃圾箱的宽度,您需要在tm_fill
函数中使用library(tmap)
library(tmaptools)
library(leaflet)
library(tidyverse)
nyc_geo = read_shape("nyu_2451_36743.shp",as.sf = T,stringsAsFactors = F)
nyc_zone <- nyc_geo[4]
#I just made up some sample data for the purposes of this question
nyc_zone$rate = sample(seq(0,4,0.5), size=nrow(nyc_zone), replace=T)
breaks = seq(0,4,0.5)
my_map =
tm_shape(nyc_zone) +
tm_fill("rate", palette = "Oranges", breaks= breaks
,title = "pickup density (%)", id = "Taxi_zone") +
tm_borders(alpha=.4)
leaflet() %>%
setView(lng = -74.058913, lat = 40.689852, zoom = 10) %>%
addProviderTiles("Esri.WorldGrayCanvas") + my_map
参数:
SpatialPolygonsDataFrame
会产生以下情节:
对于问题的第二部分,您想给Leaflet一个tmap
而不是您现在使用的library(leaflet)
library(sp)
nyc_map <- rgdal::readOGR(dsn = "nyu_2451_36743.shp", layer = "nyu_2451_36743")
nyc_map$rate = sample(seq(0,4,0.5), size=nrow(nyc_map), replace=T)
pal <- colorBin(palette = "Oranges", domain = nyc_map$rate, bins = breaks)
leaflet(nyc_map) %>%
addPolygons(fillColor = ~pal(rate),
weight = 2,
opacity = 1,
color = "white",
fillOpacity = 0.7) %>%
addProviderTiles("Esri.WorldGrayCanvas") %>%
addLegend(pal = pal, values = ~rate, opacity = 0.7, title = NULL,
position = "bottomright")
元素。这是读取shapefile然后添加感兴趣的图块的基本操作:
{{1}}
这是该地图的静态图片: