【发布时间】:2018-06-14 14:23:21
【问题描述】:
我正在尝试使用geojson 和ggplot2 将various districts of Nepal 与R 中的等值线图映射到Human Poverty Index。
我阅读了尼泊尔的geojson 数据,其中地区为from 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 package (v0.2.0) 于 2018 年 6 月 14 日创建。
如果我可以将spdf_fortified 和data 合并为merged_df,我想我可以使用此代码获得叶绿体图:
ggplot(data = merged_df, aes(x = long, y = lat, group = group)) + geom_polygon(aes(fill = Value), color = 'gray', size = 0.1)
对合并两个数据有帮助吗?
【问题讨论】: