我有一个data.frame
,其中有两(2)行。我正在尝试使用colorBin
leaflet
函数将此颜色映射为数据
my_data <- data.frame("11772","8600000US11772","11772","41957005","1150010","Patchogue","Suffolk","195")
my_data2 <- data.frame("11933","8600000US11933","11933","71811983","6255304","Calverton","Suffolk","1")
names(my_data) <- c("ZipCode", "AFFGEOID10", "GEOID10","ALAND10","AWATER10","City","County","dsch_count")
names(my_data2) <- c("ZipCode", "AFFGEOID10", "GEOID10","ALAND10","AWATER10","City","County","dsch_count")
df <- rbind(my_data, my_data2)
dsch_count_shp <- sp::merge(
x = usa
, y = df
, all.x = F
)
# Test Map
sv_lng <- -72.97659
sv_lat <- 40.78007
sv_zoom <- 9
pal <- colorBin(
#palette = "BuPu"
palette = "Dark2"
, domain = dsch_count_by_city$dsch_count
, bins = 6
, reverse = FALSE
)
popup <- paste(
"<strong>County: </strong>"
, dsch_count_shp$County
, "<br><strong>City: </strong>"
, dsch_count_shp$City
, "<br><strong>Bin: </strong>"
, dsch_count_shp$dsch_bin_test
, "<br><strong>Discharges: </strong>"
, dsch_count_shp$dsch_count
)
l <- leaflet(data = dsch_count_shp) %>%
setView(lng = sv_lng, lat = sv_lat, zoom = sv_zoom) %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(
data = dsch_count_shp
, fillColor = ~pal(as.numeric(dsch_count_by_city$dsch_count))
, fillOpacity = 1
, color = "#BDBDC3"
, weight = 0.7
, popup = popup
) %>%
addLayersControl(
baseGroups = c("OSM (default)", "CartoDB.Positron")
, options = layersControlOptions(
collapsed = FALSE
, position = "topright"
)
) %>%
addLegend(
"topright"
, pal = pal
, values = ~dsch_count_by_city$dsch_count
, title = "Discharge Bin"
, opacity = 1
)
l
我一直在地图上得到错误的颜色。形状文件很好,我是从美国人口普查局下载的,不了解为什么我的垃圾箱未正确进行颜色映射。
如果我需要将dsch_count_by_city和dsch_count_shp作为文件上传,我可以尝试这样做
答案 0 :(得分:1)
我认为您可以使用此功能: 根据您的数据自定义“ bins <-c(0.2,5,10,15,20,25,30,35,40)”
答案 1 :(得分:0)
通过以下操作解决了我的问题:
pal <- colorBin(
#palette = "BuPu"
palette = "Dark2"
#, domain = dsch_count_by_city$dsch_count
, domain = dsch_count_shp$dsch_count
, bins = 5
, reverse = TRUE
)
popup <- paste(
"<strong>County: </strong>"
, dsch_count_shp$County
, "<br><strong>City: </strong>"
, dsch_count_shp$City
# , "<br><strong>Bin: </strong>"
# , dsch_count_shp$dsch_bin_test
, "<br><strong>Discharges: </strong>"
, dsch_count_shp$dsch_count
)
l <- leaflet(data = dsch_count_shp) %>%
setView(lng = sv_lng, lat = sv_lat, zoom = sv_zoom) %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(
data = dsch_count_shp
#, fillColor = ~pal(dsch_count_by_city$dsch_count)
, fillColor = ~pal(dsch_count)
, fillOpacity = 0.7
# , color = "#BDBDC3"
, weight = 0.7
, popup = popup
) %>%
addLayersControl(
baseGroups = c("OSM (default)", "CartoDB.Positron")
, options = layersControlOptions(
collapsed = FALSE
, position = "topright"
)
) %>%
addLegend(
"topright"
, pal = pal
#, values = ~dsch_count_by_city$dsch_count
, values = ~dsch_count
, title = "Discharge Bin"
, opacity = 1
)
l