Chloropleth地图与geojson和ggplot2

Sum*_*nal 3 r ggplot2 geojson

我试图映射Human Poverty Index尼泊尔各区使用以GeoJSON和GGPLOT2 R中地区分布图.

从这里读到geojson尼泊尔的数据.

我看到一些例子在这里,在这里.

这就是我做的:

# 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()
Run Code Online (Sandbox Code Playgroud)

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
#
#
#
#
Run Code Online (Sandbox Code Playgroud)

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

如果我可以合并spdf_fortifieddatamerged_df,我想我可以得到chloroplethr地图这样的代码:

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

合并两个数据有什么帮助吗?

ali*_*ire 6

不要颠覆整个系统,但我最近一直在和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')
Run Code Online (Sandbox Code Playgroud)

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

ggrepel::geom_text_repel 有可能避免重叠标签.