【问题标题】:How to plot country-based choropleths using leaflet R如何使用传单 R 绘制基于国家/地区的等值线
【发布时间】:2020-03-10 05:24:41
【问题描述】:

世界边界geo.json从这里下载。https://github.com/johan/world.geo.json

我正在尝试突出显示 3 个国家/地区(在世界地图视图中)并根据该国家/地区的项目数量以渐变颜色绘制它们。

这是我的步骤:

首先下载世界边界geo.json文件并作为底图读取; 然后我尝试在我的数据中突出显示国家多边形。然而事实证明,世界上所有的国家都被这三个国家的信息随机着色和标记。是地理数据框子集问题吗?

WorldCountry <-geojsonio::geojson_read("./GeoData/countries.geo.json", what = "sp")

#Dataframe for choropleth map
Country <- c("Bulgaria","Pakistan","Turkey")
Projects <- c(2,1,6)
data <- data.frame(Country,Projects)

#basemap
Map <- leaflet(WorldCountry) %>% addTiles() %>% addPolygons()

#set bin and color for choropleth map
bins <- c(0,1,2,3,4,5,6,7,8,9,10,Inf)
pal <- colorBin("YlOrRd", domain = data$Projects, bins = bins)

#set labels
labels <- sprintf(
  "<strong>%s</strong><br/>%g projects <sup></sup>",
  data$Country, data$Projects) %>% lapply(htmltools::HTML)

#add polygons,labels and mouse over effect
Map %>% addPolygons(
  fillColor = ~pal(data$Projects),
  weight = 2,
  opacity = 1,
  color = 'white',
  dashArray = '3',
  fillOpacity = 0.7,
  highlight = highlightOptions(
     weight = 5,
    color = "#666",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE),
  label = labels,
  labelOptions = labelOptions(
    style = list("font-weight" = "normal", padding = "3px 8px"),
    textsize = "15px",
    direction = "auto")
)

我期待这样的事情:

【问题讨论】:

    标签: json r leaflet geospatial


    【解决方案1】:

    这样就可以了!使用以下方法对 WorldCountry 进行子集:

    data_Map <- WorldCountry[WorldCountry$id %in% data$Country, ]
    Map <- leaflet(data_Map) %>% addTiles() %>% addPolygons()
    

    【讨论】:

      【解决方案2】:

      子集将是 WorldCountry$name

      data_Map <- WorldCountry[WorldCountry$name %in% data$Country, ]
      
      Map <- leaflet(data_Map) %>% addTiles() %>% addPolygons(
        fillColor = ~pal(data$Projects),
        weight = 2,
        opacity = 1,
        color = 'white',
        dashArray = '3',
        fillOpacity = 0.7,
        highlight = highlightOptions(
          weight = 5,
          color = "#666",
          dashArray = "",
          fillOpacity = 0.7,
          bringToFront = TRUE),
        label = labels,
        labelOptions = labelOptions(
          style = list("font-weight" = "normal", padding = "3px 8px"),
          textsize = "15px",
          direction = "auto")
      )
      

      【讨论】:

        猜你喜欢
        • 2017-08-22
        • 2018-06-20
        • 1970-01-01
        • 1970-01-01
        • 2022-11-18
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        相关资源
        最近更新 更多