gpt4 book ai didi

r - 带 geojson 和 ggplot2 的等值线图

转载 作者:行者123 更新时间:2023-12-04 11:33:21 25 4
gpt4 key购买 nike

我正在尝试映射 Human Poverty Indexvarious districts of Nepal带有等值线 map R使用 geojsonggplot2 .

我读了 geojson尼泊尔地区数据 from here .

我看到了一些例子 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
#
#
#
#

创建于 2018-06-14 由 reprex package (v0.2.0)。

如果我可以合并 spdf_fortifieddata进入 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 变成了一个 choropleth :

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是一种避免重叠标签的可能性。

关于r - 带 geojson 和 ggplot2 的等值线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50859765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com