# France map with ggiraph ------------------------------------------------- #### 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 ) 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 ) 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 for illustration purposes 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 ) #### ggplot2 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") ggplot(france_dept_fort) + geom_polygon(aes(x = long, y = lat, group = group, fill = nbre_naissances_cat), color = "grey") + geom_path(data = france_reg_fort, aes(x = long, y = lat, group = group), color = "black") + geom_point(data = villes_france, aes(x = long, y = lat, size = pop)) + 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()) #### ggplot2 + ggiraph map #### # 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( "", conv_accents(france_dept_fort$region), " : ", prettyNum(france_dept_fort$nbre_naissances, big.mark = " "), " naissances", "
(", 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() ) ggiraph(code = {print(gg)}, width = 22, height = 10, hover_css = "{fill:orange;}")