3

我有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 作为文件,我可以尝试这样做

4

2 回答 2

2

I think you can use this: customize "bins <- c(0.2, 5, 10, 15, 20, 25, 30, 35, 40)" the numbers according to your data

于 2020-07-02T04:53:14.517 回答
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
于 2018-08-07T19:23:22.503 回答