Carte de France des naissances en 2014 réalisée avec ggplot2 et rendue interactive avec ggiraph.
Les contours des régions et des départements utilisés sont des shapefiles provenant de data.gouv.fr :
Les données sur les naissances par départements proviennent également de data.gouv.fr :
Code is available in this gist.
#### Packages ####
library("ggplot2")
library("ggiraph")
library("ggthemes")
library("rgdal")
library("readxl")
library("maptools")
library("dplyr")
library("mapproj")
library("maps")
#### Data ####
### Shapefiles from OSM via data.gouv.fr
# https://www.data.gouv.fr/fr/datasets/contours-des-regions-francaises-sur-openstreetmap/
# https://www.data.gouv.fr/fr/datasets/contours-des-departements-francais-issus-d-openstreetmap/
# dir.create("shapefiles")
## French departements
# download.file(
# url = "http://osm13.openstreetmap.fr/~cquest/openfla/export/departements-20140306-100m-shp.zip",
# destfile = "shapefiles/departements-20140306-100m-shp.zip"
# )
# unzip(zipfile = "shapefiles/departements-20140306-100m-shp.zip",
# exdir = "shapefiles/departements-20140306-100m-shp")
france_dept <- readOGR(
dsn = "shapefiles/departements-20140306-100m-shp",
layer = "departements-20140306-100m", stringsAsFactors = FALSE
)
## OGR data source with driver: ESRI Shapefile
## Source: "shapefiles/departements-20140306-100m-shp", layer: "departements-20140306-100m"
## with 101 features
## It has 4 fields
france_dept <- france_dept[france_dept@data$code_insee %in% sprintf("%02d", (1:95)[-20]), ]
## French regions
# download.file(
# url = "http://osm13.openstreetmap.fr/~cquest/openfla/export/regions-20140306-100m-shp.zip",
# destfile = "shapefiles/regions-20140306-100m-shp.zip"
# )
# unzip(zipfile = "shapefiles/regions-20140306-100m-shp.zip",
# exdir = "shapefiles/departements-20140306-100m-shp")
france_reg <- readOGR(
dsn = "shapefiles/departements-20140306-100m-shp",
layer = "regions-20140306-100m", stringsAsFactors = FALSE
)
## OGR data source with driver: ESRI Shapefile
## Source: "shapefiles/departements-20140306-100m-shp", layer: "regions-20140306-100m"
## with 27 features
## It has 10 fields
france_reg <- france_reg[substr(france_reg@data$insee_cl, 1, 2) %in% sprintf("%02d", (1:95)[-20]), ]
## Some cities
data("world.cities", package = "maps")
villes_france <- world.cities %>% filter(country.etc == "France") %>%
top_n(20, pop) %>%
mutate(tip = paste0(name, " : ", prettyNum(pop, big.mark = " "), " habitants"))
### Some data about births
# https://www.data.gouv.fr/fr/datasets/nombre-de-naissances-par-departement/
# download.file(
# url = "https://www.data.gouv.fr/s/resources/nombre-de-naissances-par-departement/20150909-103422/naissanceok.xlsx",
# destfile = "naissanceok.xlsx", mode = "wb"
# )
naissances <- read_excel(path = "naissanceok.xlsx")
naissances <- naissances[, c(1, 4, 3)]
names(naissances) <- c("code_insee", "nbre_naissances", "evolution")
naissances <- naissances[naissances$code_insee %in% sprintf("%02d", (1:95)[-20]), ]
qu <- quantile(x = naissances$nbre_naissances, probs = seq(from = 0, to = 1, length.out = 6))
qu <- round(qu)
naissances$nbre_naissances_cat <- cut(
x = naissances$nbre_naissances,
breaks = qu, labels = paste(qu[-length(qu)], qu[-1], sep = " - "),
include.lowest = TRUE
)
#### Map ####
# Fortify polygons
france_dept_fort <- fortify(france_dept, region = "code_insee")
france_dept_fort <- left_join(
x = france_dept_fort,
y = naissances,
by = c("id" = "code_insee")
)
france_reg_fort <- fortify(france_reg, region = "insee_cl")
# add dpt name
france_dept_fort <- left_join(
x = france_dept_fort,
y = france_dept@data %>% select(code_insee, region = nom),
by = c("id" = "code_insee")
)
# tooltip (for accent in html)
conv_accents <- function(x) {
x <- gsub(pattern = "è", replacement = "è", x = x)
x <- gsub(pattern = "é", replacement = "é", x = x)
x <- gsub(pattern = "ê", replacement = "ê", x = x)
x <- gsub(pattern = "ô", replacement = "ô", x = x)
x <- gsub(pattern = "'", replacement = "´", x = x)
return(x)
}
france_dept_fort$tip <- paste0(
"<b>", conv_accents(france_dept_fort$region), "</b> : ",
prettyNum(france_dept_fort$nbre_naissances, big.mark = " "), " naissances",
"<br>(", ifelse(france_dept_fort$evolution > 0, "+", ""), round(france_dept_fort$evolution),
"% par rapport à 2003)"
)
gg <- ggplot(france_dept_fort) +
geom_polygon_interactive(
aes(x = long, y = lat, group = group, fill = nbre_naissances_cat,
tooltip = tip, use_jquery = TRUE, data_id = id),
color = "grey"
) +
geom_path(
data = france_reg_fort, aes(x = long, y = lat, group = group), color = "black"
) +
geom_point_interactive(
data = villes_france, aes(x = long, y = lat, size = pop, tooltip = tip, data_id = name)
) +
coord_map(projection = "mercator") +
scale_fill_manual(
values = colorRampPalette(c("#FFFFCC", "#FFA083"), space = "Lab")(5),
guide = guide_legend(title = "Nombre de naissances")
) +
scale_size(
guide = guide_legend(title = "Nombre d'habitants"),
breaks = c(500000, 1000000, 2000000),
labels = c("500 000", "1 000 000", "2 000 000")
) +
theme_tufte(ticks = FALSE) +
theme(
axis.text = element_blank(),
axis.title = element_blank()
) +
ggtitle("Naissances en 2014")
ggiraph(code = {print(gg)}, width = 14, height = 10, hover_css = "{fill:orange;}")
sessionInfo()
## R version 3.2.3 (2015-12-10)
## Platform: i386-w64-mingw32/i386 (32-bit)
## Running under: Windows 10 x64 (build 10586)
##
## locale:
## [1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252
## [3] LC_MONETARY=French_France.1252 LC_NUMERIC=C
## [5] LC_TIME=French_France.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] gdtools_0.0.6 mapproj_1.2-4 maps_3.0.2 dplyr_0.4.3
## [5] maptools_0.8-39 readxl_0.1.0 rgdal_1.1-3 sp_1.2-2
## [9] ggthemes_3.0.1 ggiraph_0.1.0 ggplot2_2.0.0
##
## loaded via a namespace (and not attached):
## [1] digest_0.6.9 htmltools_0.3 R6_2.1.2
## [4] scales_0.3.0 assertthat_0.1 grid_3.2.3
## [7] stringr_1.0.0 R.utils_2.2.0 knitr_1.12.3
## [10] munsell_0.4.2 rvg_0.0.6 rgeos_0.3-15
## [13] lattice_0.20-33 DBI_0.3.1 xml2_0.1.2
## [16] labeling_0.3 R.methodsS3_1.7.0 jsonlite_0.9.19
## [19] plyr_1.8.3 stringi_1.0-1 magrittr_1.5
## [22] rmarkdown_0.9.2 evaluate_0.8 gtable_0.1.2
## [25] colorspace_1.2-6 foreign_0.8-66 yaml_2.1.13
## [28] tools_3.2.3 parallel_3.2.3 htmlwidgets_0.5
## [31] lazyeval_0.1.10 R.oo_1.19.0 formatR_1.2.1
## [34] Rcpp_0.12.3