I am trying to map Human Poverty Index
for various districts of Nepal with choropleth map in R
using geojson
and ggplot2
.
I read geojson
data for Nepal with districts from here.
I saw some examples here, here.
This is what I did:
# 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
#
#
#
#
Created on 2018-06-14 by the reprex package (v0.2.0).
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 ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…