Using the geofacet package to spatially arrange plots

[This article was first published on r.iresmi.net, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)


Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.

The {geofacet} package allows to « arrange a sequence of plots of data for different geographical entities into a grid that strives to preserve some of the original geographical orientation of the entities ».

Like the previous post, it’s interesting if you view each entity as a unit and don’t care for its real size or weight, and don’t want to spend too much time manually finding the best grid.

We will again use the same COVID-19 dataset. We manually add the overseas départements once we have found the right grid (by trying different seeds) and adjust Corsica position.

COVID-19 deceased in hospital, by département, for 100 000 inhab.

# packages ----------------------------------------------------------------
library(tidyverse)
library(httr)
library(fs)
library(sf)
library(readxl)
library(janitor)
library(glue)
library(geofacet)
# also install ragg # sources ----------------------------------------------------------------- # https://www.data.gouv.fr/fr/datasets/donnees-hospitalieres-relatives-a-lepidemie-de-covid-19/
fichier_covid <- "donnees/covid.csv"
url_donnees_covid <- "https://www.data.gouv.fr/fr/datasets/r/63352e38-d353-4b54-bfd1-f1b3ee1cabd7" # https://www.insee.fr/fr/statistiques/2012713#tableau-TCRD_004_tab1_departements
fichier_pop <- "donnees/pop.xls"
url_donnees_pop <- "https://www.insee.fr/fr/statistiques/fichier/2012713/TCRD_004.xls" # Adminexpress : à télécharger manuellement
# https://geoservices.ign.fr/documentation/diffusion/telechargement-donnees-libres.html#admin-express
aex <- path_expand("~/Downloads/ADMIN-EXPRESS_2-2__SHP__FRA_2020-02-24/ADMIN-EXPRESS/1_DONNEES_LIVRAISON_2020-02-24") # config ------------------------------------------------------------------ options(scipen = 999) force_download <- FALSE # retélécharger même si le fichier existe et a été téléchargé aujourd'hui ? # téléchargement ------------------------------------------------- if (!dir_exists("donnees")) dir_create("donnees")
if (!dir_exists("resultats")) dir_create("resultats")
if (!dir_exists("resultats/animation_spf")) dir_create("resultats/animation_spf") if (!file_exists(fichier_covid) | file_info(fichier_covid)$modification_time < Sys.Date() | force_download) { GET(url_donnees_covid, progress(), write_disk(fichier_covid, overwrite = TRUE)) %>% stop_for_status()
} if (!file_exists(fichier_pop)) { GET(url_donnees_pop, progress(), write_disk(fichier_pop)) %>% stop_for_status()
} covid <- read_csv2(fichier_covid) pop <- read_xls(fichier_pop, skip = 2) %>% clean_names() # adminexpress prétéléchargé
dep <- read_sf(path(aex, "ADE_2-2_SHP_LAMB93_FR/DEPARTEMENT.shp")) %>% clean_names() %>% st_set_crs(2154) # construction de la grille ---------------------------------------- grid_fr <- dep %>% select(insee_dep, nom_dep) %>% grid_auto(names = "nom_dep", codes = "insee_dep", seed = 4) %>% add_row(row = 8, col = 1, name_nom_dep = "Guadeloupe", code_insee_dep = "971") %>% add_row(row = 9, col = 1, name_nom_dep = "Martinique", code_insee_dep = "972") %>% add_row(row = 10, col = 1, name_nom_dep = "Guyane", code_insee_dep = "973") %>% add_row(row = 7, col = 13, name_nom_dep = "Mayotte", code_insee_dep = "976") %>% add_row(row = 8, col = 13, name_nom_dep = "La Réunion", code_insee_dep = "974") grid_fr[grid_fr$code_insee_dep %in% c("2A", "2B"), "col"] <- 13
grid_fr[grid_fr$code_insee_dep %in% c("2A", "2B"), "row"] <- grid_fr[grid_fr$code_insee_dep %in% c("2A", "2B"), "row"] - 1 # graphique ----------------------------------------------------- df <- covid %>% filter(sexe == 0) %>% rename(deces = dc, reanim = rea, hospit = hosp) %>% left_join(pop, by = c("dep" = "x1")) %>% mutate(incidence = deces / x2020_p * 100000) %>% rename(insee_dep = dep) %>% left_join(grid_fr %>% select(nom_dep = name_nom_dep, insee_dep = code_insee_dep)) %>% drop_na(insee_dep) %>% ggplot(aes(jour, incidence)) + geom_area() + facet_geo(~ nom_dep, grid = grid_fr) + labs(title = "Mortalité", subtitle = "COVID-19 - France", x = "date", y = "décès pour\n100 000 hab.", caption = glue("http://r.iresmi.net/\ndonnées SPF {Sys.Date()}")) + theme_minimal() + theme(strip.text = element_text(hjust = 0, size = 7)) ggsave(glue("resultats/covid_fr_mortalite_geofacette_{Sys.Date()}.png"), width = 25, height = 20, units = "cm", scaling = .8, res = 300, device = ragg::agg_png)



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook
Favorite

Leave a Comment