0

I have the dataframe below for which I want to create a chorpleth map. I downloaded the germany shapefile from here and then I use this code to create the map. As you can see the map is created but because I have several regions missing they are set to NAs and they get a black color. How can I deal with this issue? Maybe eliminate them or change them to 0? Im open to other packages like leaflet or something if they can solve the issue.

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()

enter image description here

2 Answers 2

2

Package sf will make your process easier.

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()

ggplot map

For a zoomable/interactive option, use {tmap}, a package that wraps {leaflet} for quick, simple maps.

library(tmap)
tmap_mode("view")

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

leaflet map

2
  • nice method can this be somehow interactive with ability to zoom?
    – firmo23
    Commented Mar 16, 2021 at 12:33
  • great option.I suppose this can be embedded in a shiny app
    – firmo23
    Commented Mar 17, 2021 at 9:31
1

I've been messing around with the choroplethr package a bit and had this same question. The "aha" moment was learning that the output from the various x_choropleth functions is actually just a ggplot object. This means you can modify them as you would any ggplot graphic. So if you add something like this in your graphic output pipeline I think it might achieve what you're after:

+ scale_fill_distiller(na.value = "white")

Not sure if some of the other things you're doing here would preclude this from working.

Shout out to this write-up: https://statisticaloddsandends.wordpress.com/2019/07/15/looking-at-flood-insurance-claims-with-choroplethr/

Not the answer you're looking for? Browse other questions tagged or ask your own question.