【问题标题】:Chloropleth map with geojson and ggplot2带有 geojson 和 ggplot2 的 Choropleth 地图
【发布时间】:2018-06-14 14:23:21
【问题描述】:

我正在尝试使用geojsonggplot2various districts of NepalR 中的等值线图映射到Human Poverty Index

我阅读了尼泊尔的geojson 数据,其中地区为from here

我看到了一些例子herehere

这就是我所做的:

# 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_fortifieddata 合并为merged_df,我想我可以使用此代码获得叶绿体图:

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

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

【问题讨论】:

    标签: r ggplot2 geojson


    【解决方案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 可以避免标签重叠。

    【讨论】:

    • 清晰的解释。谢谢你。有没有办法给地区添加标签。
    • 当然,geom_textgeom_label。不过,它们需要xy 美学,因此您需要事先计算一些,例如使用sf::st_centroid(您仍需要解压缩其结果)。我已经在上面进行了编辑以进行演示。
    猜你喜欢
    • 2021-02-19
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2017-12-20
    • 2018-08-19
    • 2015-01-02
    相关资源
    最近更新 更多