【问题标题】:Deal with missing regions which provoke NAs in choropleth map处理在等值线图中引起 NA 的缺失区域
【发布时间】:2021-06-13 04:23:32
【问题描述】:

我有下面的数据框,我想为其创建一个 chorpleth 地图。我从here 下载了德国 shapefile,然后使用此代码创建地图。如您所见,地图已创建,但由于我缺少几个区域,它们被设置为NAs,并且它们变成黑色。我该如何处理这个问题?也许消除它们或将它们更改为0?如果可以解决问题,我愿意接受其他软件包,例如 leaflet 或其他软件包。

region<-c("09366", 
           "94130", 
           "02627", 
           "95336", 
           "08525", 
           "92637", 
           "95138", 
           "74177", 
           "08606", 
           "94152" )


value<-c( 39.5, 
            519.,  
              5.67,
              5.10,
              5.08,
           1165,  
            342,  
            775,  
           3532,  
             61.1 )

df<-data.frame(region,value)




#shapefile from http://www.suche-postleitzahl.org/downloads?download=zuordnung_plz_ort.csv
library(choroplethr)
library(dplyr)
library(ggplot2)
library(rgdal)
library(maptools)
library(gpclib)
library(readr)
library(R6)
ger_plz <- readOGR(dsn = ".", layer = "plz-gebiete")
gpclibPermit()
#convert the raw data to a data.frame as ggplot works on data.frames
ger_plz@data$id <- rownames(ger_plz@data)
ger_plz.point <- fortify(ger_plz, region="id")
ger_plz.df <- inner_join(ger_plz.point,ger_plz@data, by="id")
head(ger_plz.df)
ggplot(ger_plz.df, aes(long, lat, group=group )) + geom_polygon()
#data file
#df <- produce_sunburst_sequences
# variable name 'region' is needed for choroplethr
ger_plz.df$region <- ger_plz.df$plz
head(ger_plz.df)
#subclass choroplethr to make a class for your my need
GERPLZChoropleth <- R6Class("GERPLZChoropleth",
                            inherit = choroplethr:::Choropleth,
                            public = list(
                              initialize = function(user.df) {
                                super$initialize(ger_plz.df, user.df)
                              }
                            )
)
#df<-df[,c(6,13)]
#choropleth needs these two columnames - 'region' and 'value'
colnames(df) = c("region", "value")
#df<-df[!(df$region=="Missing_company_zip"),]
#df<-df[!duplicated(df$region), ]
#instantiate new class with data
c <- GERPLZChoropleth$new(df)
#plot the data
c$ggplot_polygon = geom_polygon(aes(fill = value), color = NA)
c$title = "Comparison of number of Inhabitants per Zipcode in Germany"
c$legend= "Number of Inhabitants per Zipcode"
c$set_num_colors(9)
c$render()

【问题讨论】:

    标签: r dictionary leaflet choropleth


    【解决方案1】:

    包裹sf将使您的过程更轻松。

    library(tidyverse)
    library(sf)
    
    df <- data.frame(region = c("09366", "94130", "02627", "95336", "08525", "92637", "95138", "74177", "08606", "94152"), 
                     value  = c(39.5, 519, 5.67, 5.1, 5.08, 1165, 342, 775, 3532, 61.1))
    
    germany_sf <- sf::st_read(dsn = "plz-gebiete.shp") %>% 
      left_join(df, by = c("plz" = "region"))
    
    germany_sf %>%
      ggplot() +
        geom_sf(alpha = 0.1, size = 0.1, colour = "gray") +
        geom_sf(data = . %>% filter(!is.na(value)), aes(fill = value)) +
        scale_fill_viridis_c() +
        theme_bw()
    

    对于可缩放/交互式选项,请使用 {tmap},这是一个包装 {leaflet} 的包,用于快速、简单的地图。

    library(tmap)
    tmap_mode("view")
    
    tm_shape(shp = germany_sf) + 
      tm_polygons(col = "value", border.alpha = 0)
    

    【讨论】:

    • 不错的方法可以以某种方式与缩放功能交互吗?
    • 不错的选择。我想这可以嵌入到闪亮的应用程序中
    【解决方案2】:

    我一直在搞乱 choroplethr 包,并且有同样的问题。 “啊哈”时刻正在学习各种 x_choropleth 函数的输出实际上只是一个 ggplot 对象。这意味着您可以像修改任何 ggplot 图形一样修改它们。因此,如果您在图形输出管道中添加类似的内容,我认为它可能会实现您所追求的:

    + scale_fill_distiller(na.value = "white")
    

    不确定你在这里做的其他一些事情是否会妨碍它工作。

    为这篇文章大声疾呼:https://statisticaloddsandends.wordpress.com/2019/07/15/looking-at-flood-insurance-claims-with-choroplethr/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-15
      • 2015-07-03
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多