[英]How to plot country-based choropleths using leaflet R
世界邊界 geo.json 從這里下載。 https://github.com/johan/world.geo.json
我正在嘗試突出顯示 3 個國家/地區(在世界地圖視圖中)並根據該國家/地區的項目數量以漸變顏色繪制它們。
這是我的步驟:
首先下載世界邊界geo.json文件,作為底圖讀取; 然后我嘗試在我的數據中突出顯示國家多邊形。 然而,事實證明,世界上所有的國家都是隨機着色的,並被 3 個國家的信息標記。 是地理數據框子集問題嗎?
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")
)
我期待這樣的事情:
這將奏效! 子集 WorldCountry 使用:
data_Map <- WorldCountry[WorldCountry$id %in% data$Country, ]
Map <- leaflet(data_Map) %>% addTiles() %>% addPolygons()
子集將與 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.