带有 geojson 和 ggplot2 的等值线图
Chloropleth map with geojson and ggplot2
我正在尝试使用 geojson
和 ggplot2
.
将 Human Poverty Index
映射到 R
中的等值线图 various districts of Nepal
我阅读了 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-06-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)
对合并两个数据有帮助吗?
并不是要颠覆你的整个系统,但我最近一直在使用 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
可以避免标签重叠。
我正在尝试使用 geojson
和 ggplot2
.
Human Poverty Index
映射到 R
中的等值线图 various districts of Nepal
我阅读了 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-06-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)
对合并两个数据有帮助吗?
并不是要颠覆你的整个系统,但我最近一直在使用 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
可以避免标签重叠。