代码之家  ›  专栏  ›  技术社区  ›  Suman

带有geojson和ggplot2的叶绿体图

  •  2
  • Suman  · 技术社区  · 7 年前

    我在画地图 Human Poverty Index 对于 various districts of Nepal 使用geojson和ggplot2在r中使用choropleth映射。

    我读 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_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)
    

    合并两个数据有什么帮助吗?

    1 回复  |  直到 7 年前
        1
  •  6
  •   alistaire    7 年前

    不是要颠覆你的整个系统,但我一直在和 sf 最近,发现它比sp.ggplot更容易使用。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 有可能避免标签重叠。