Chloropleth地图与geojson和ggplot2

时间:2018-06-14 14:23:22

标签: r ggplot2 geojson

我正在尝试使用geojson和ggplot2将Human Poverty Index various districts of Nepal映射到R中的等值区映射。

我为地区from here阅读了尼泊尔的geojson数据。

我看到了一些示例herehere

这就是我所做的:

# Read geojson data for nepal with districts
library(tidyverse)
library(geojsonio)
#> 
#> Attaching package: 'geojsonio'
#> The following object is masked from 'package:base':
#> 
#>     pretty
spdf <- geojson_read("nepal-districts.geojson",  what = "sp")
##https://github.com/mesaugat/geoJSON-Nepal/blob/master/nepal-districts.geojson




#tidy data for ggplot2
library(broom)
spdf_fortified <- tidy(spdf)
#> Regions defined for each Polygons

# plot
ggplot() +
    geom_polygon(data = spdf_fortified, aes( x = long, y = lat, group = group)) +
    theme_void() +
    coord_map()

names(spdf_fortified)
#> [1] "long"  "lat"   "order" "hole"  "piece" "group" "id"



#Now read the data to map to districts
data=read.csv("data.csv")
#data from here
#https://github.com/opennepal/odp-poverty/blob/master/Human%20Poverty%20Index%20Value%20by%20Districts%20(2011)/data.csv

#filter and select data to reflect Value of HPI in various districts
data <- data %>% filter(Sub.Group=="HPI") %>% select(District,Value)


head(data)
#>       District Value
#> 1       Achham 46.68
#> 2 Arghakhanchi 27.37
#> 3        Banke 32.10
#> 4      Baglung 27.33
#> 5      Baitadi 39.58
#> 6      Bajhang 45.32

# Value represents HPI value for each district.

#Now how to merge and fill Value for various districts
#
#
#
#

reprex package(v0.2.0)创建于2018-06-14。

如果我可以将spdf_fortifieddata合并到merged_df,我想我可以使用此代码获取chloroplethr地图:

ggplot(data = merged_df, aes(x = long, y = lat, group = group)) + geom_polygon(aes(fill = Value), color = 'gray', size = 0.1)

合并两个数据的任何帮助?

1 个答案:

答案 0 :(得分:5)

不要颠覆整个系统,但我最近一直在与sf合作,并且发现它比sp更容易使用。 ggplot也有很好的支持,所以你可以用geom_sf进行绘图,通过将变量映射到fill来变成一个等值区:

library(sf)
library(tidyverse)

nepal_shp <- read_sf('https://raw.githubusercontent.com/mesaugat/geoJSON-Nepal/master/nepal-districts.geojson')
nepal_data <- read_csv('https://raw.githubusercontent.com/opennepal/odp-poverty/master/Human%20Poverty%20Index%20Value%20by%20Districts%20(2011)/data.csv')

# calculate points at which to plot labels
centroids <- nepal_shp %>% 
    st_centroid() %>% 
    bind_cols(as_data_frame(st_coordinates(.)))    # unpack points to lat/lon columns

nepal_data %>% 
    filter(`Sub Group` == "HPI") %>% 
    mutate(District = toupper(District)) %>% 
    left_join(nepal_shp, ., by = c('DISTRICT' = 'District')) %>% 
    ggplot() + 
    geom_sf(aes(fill = Value)) + 
    geom_text(aes(X, Y, label = DISTRICT), data = centroids, size = 1, color = 'white')

在两个数据框架中,有三个区域的名称不同,必须进行清理,但如果没有大量工作,这是一个非常好的起点。

ggrepel::geom_text_repel可以避免重叠标签。