2017-06-13 41 views
1

世界范围geo.json从这里下载。 https://github.com/johan/world.geo.json如何使用传单绘制基于国家的choropleths R

我想突出显示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") 
) 

我期待这样的事情:

enter image description here

回答

1

这将这样的伎俩!子世界国家使用:

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