Created
November 6, 2019 16:01
-
-
Save rCarto/ebdae9babbcd4a83e1a5ae5a2f0d59ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# World basemap | |
download.file(url = "https://raw.githubusercontent.com/riatelab/basemaps/master/World/countries.geojson", | |
destfile = "country.geojson") | |
# Graticules layer | |
download.file(url = "https://raw.githubusercontent.com/riatelab/basemaps/master/World/graticule30.geojson", | |
destfile = "graticule.geojson") | |
# Population data base | |
download.file(url = "https://population.un.org/wup/Download/Files/WUP2018-F12-Cities_Over_300K.xls", | |
destfile = "citypop.xls") | |
# libraries | |
library(sf) | |
library(cartography) | |
library(readxl) | |
# import | |
country <- st_read(dsn = "country.geojson", quiet = TRUE) | |
graticule <- st_read(dsn = "graticule.geojson", quiet = TRUE) | |
city <- data.frame(read_excel("citypop.xls", skip = 16)) | |
# construction de la couche city | |
city <- st_as_sf(city, | |
coords = c("Longitude", "Latitude"), | |
crs = 4326) | |
# transformation de la projection WGS84 => Robinson | |
country <- st_transform(x = country, crs = 54030) | |
graticule <- st_transform(x = graticule, crs = 54030) | |
city <- st_transform(x = city, crs = 54030) | |
# selection des 50 plus grandes villes de 2035 | |
cityx <- city[order(city$X2035, decreasing = T),][1:50, ] | |
# l'argument mfrow de la fonction par() permet de définir le découpage de la | |
# fenêtre graphique. Ici 6 lignes sur 3 colonnes. | |
par(mfrow = c(6, 3), mar = c(0.1, 0.1, 0.1, 0.1)) | |
# Dans cette boucle nous parcourons les noms des colonnes à représenter: | |
# names(cityx)[7:24] | |
for (i in names(cityx)[7:24]) { | |
# affichage des graticules | |
plot( | |
st_geometry(graticule), | |
col = "lightblue", | |
border = "white", | |
lwd = 0.2, | |
bg = "cornsilk2" | |
) | |
# affichage des pays | |
plot( | |
st_geometry(country), | |
col = "ivory4", | |
border = "ivory3", | |
lwd = 0.5, | |
add = TRUE | |
) | |
# affichages des symboles proportionnels | |
propSymbolsLayer( | |
x = cityx, | |
var = i, | |
col = "darkred", | |
legend.pos = "n", | |
fixmax = 43000, | |
border = "white", | |
lwd = 0.5, | |
inches = 0.1 | |
) | |
# affichage des légendes "custom" | |
legendCirclesSymbols( | |
pos = "bottomleft", | |
title.txt = "Pop (milliers)", | |
title.cex = 0.8, | |
cex = 0.7, | |
col = "darkred", | |
var = c(1000, 7000, 20000, 43000), | |
inches = 0.1 | |
) | |
# affichage de l'année | |
text( | |
x = -14900000, | |
y = 8000000, | |
labels = substr(i, 2, 5), | |
cex = 1.5, | |
font = 2 | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment