带geojson和ggplot2的Chloropleth映射 [英] Chloropleth map with geojson and ggplot2

查看:76
本文介绍了带geojson和ggplot2的Chloropleth映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Human Poverty Index )/data.csv"rel =" nofollow noreferrer>尼泊尔的各个地区,并在R中使用geojsonggplot2绘制了Choropleth映射.

I am trying to map Human Poverty Index for various districts of Nepal with choropleth map in R using geojson and ggplot2.

我从来自的地区读取了尼泊尔的geojson数据这里.

I read geojson data for Nepal with districts from here.

我看到了一些示例此处.

I saw some examples here, here.

这就是我所做的:

# 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软件包(v0.2.0)于2018-06-14创建.

Created on 2018-06-14 by the reprex package (v0.2.0).

如果我可以将spdf_fortifieddata合并为merged_df,我想我可以使用以下代码获得chloropleth映射:

If I can merge spdf_fortified and data into merged_df, I think I can get the chloropleth map with this code:

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

对合并两个数据有帮助吗?

Any help in merging two data ?

推荐答案

不要破坏您的整个系统,但是我一直在与

Not to upend your whole system, but I've been working with sf a lot lately, and have found it a lot easier to work with than sp. ggplot has good support, too, so you can plot with geom_sf, turned into a choropleth by mapping a variable to 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')

在两个数据框中,三个区的名称不同,因此必须对其进行清理,但这是一个很好的起点,无需进行大量工作.

Three of the districts are named differently in the two data frames and will have to be cleaned up, but it's a pretty good starting point without a lot of work.

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

这篇关于带geojson和ggplot2的Chloropleth映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆