简体   繁体   English

如何使用传单 R 绘制基于国家的等值线图

[英]How to plot country-based choropleths using leaflet R

World boundaries geo.json downloaded from here.世界边界 geo.json 从这里下载。 https://github.com/johan/world.geo.json https://github.com/johan/world.geo.json

I am trying to highlight 3 countries(in a world map view) and paint them in gradient color according to numbers of projects in that country.我正在尝试突出显示 3 个国家/地区(在世界地图视图中)并根据该国家/地区的项目数量以渐变颜色绘制它们。

Here're my steps:这是我的步骤:

Firstly download world boundary geo.json file and read it as the basemap;首先下载世界边界geo.json文件,作为底图读取; Then I try highlight the country polygons in my data.然后我尝试在我的数据中突出显示国家多边形。 However it turns out that All the world countries are ramdomly colored and labeled by the 3 countries' information.然而,事实证明,世界上所有的国家都是随机着色的,并被 3 个国家的信息标记。 Is it geo dataframe subsetting issue?是地理数据框子集问题吗?

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

I am expecting something like this:我期待这样的事情:

在此处输入图片说明

This will do the trick!这将奏效! Subset the WorldCountry using:子集 WorldCountry 使用:

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

The subset would be with WorldCountry$name子集将与 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")
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 folium choropleths 中正确使用 key_on - How to properly use key_on in folium choropleths 如何在传单中显示单个国家的多边形并使其居中? - How to show and center a single country polygon in leaflet? Angular 如何使用单击功能根据国家/地区名称提取和显示特定的 JSON 数据 - Angular how to extract and display specific JSON data based on country name using click function 如何基于Month对Json数据进行分组,并使用谷歌图表绘制它 - How to group Json data based on Month and plot it using google chart 使用jQuery根据国家和州填充州和城市下拉列表 - Populate state and city dropdowns based on country and state using jQuery 如何在 ReactJs 中根据所选国家创建城市下拉列表 - How To Create Cities Dropdown Based on Selected Country In ReactJs 小册子地图上不同路径的不同部分的颜色有何不同? [R Studio] - How different colors to different sections of a route on leaflet map? [R Studio] R,GeoJSON和Leaflet - R, GeoJSON and Leaflet 如何点击 R Leaflet 状态转到 url? - How to make a click on R Leaflet state go to a url? 使用jQuery自动完成插件时如何传递国家代码 - How to pass country code when using jquery autocomplete plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM