Skip to content

Instantly share code, notes, and snippets.

@datagistips
Last active December 11, 2015 17:38
Show Gist options
  • Save datagistips/4635619 to your computer and use it in GitHub Desktop.
Save datagistips/4635619 to your computer and use it in GitHub Desktop.
Pays rangés selon l'alphabet
library(rgdal)
countries <- readOGR("D:/DATAS/NATURAL EARTH/ne_110m_admin_0_countries_lakes/ne_110m_admin_0_countries_lakes.shp", "ne_110m_admin_0_countries_lakes")
first <- substr(as.character(countries$name),0,1) # on détermine la première lettre du nom du pays
first[grep("Sahara", countries$name)] <- "S"
first[grep("Guinea", countries$name)] <- "G"
first[grep("Korea", countries$name)] <- "K"
first[grep("Congo", countries$name)] <- "C"
first[grep("Antarctic", countries$name)] <- "A"
first[grep("Cyprus", countries$name)] <- "C"
nLettres <- length(unique(first)) # nombre de lettres de l'alphabet
nullplots <- max(table(first)) - table(first) # histoire de remplir la matrice dans le cas où pas beaucoup de pays pour une lettre
#####################
# MONDE EN ALPHABET #
#####################
cols <- list('Sub-Saharan Africa' = "burlywood4",
'Middle East & North Africa' = "lightgoldenrod",
'Antarctica' = "cyan4",
'South Asia' = "Brown",
'Europe & Central Asia' = "darkolivegreen",
'North America' = "cornflowerblue",
'Latin America & Caribbean' = "orangered3",
'East Asia & Pacific' = "brown2")
png(file='IMG/alphabet.png', width=3000, height=3000)
par(mfrow=c(nLettres, max(table(first))+1), bg=gray(.8), mar=c(2.5,1.5,2.5,1.5), bty="l") # une colonne en plus (+1) pour la lettre
sapply(1:nLettres, function(x) {
lettre <- levels(as.factor(first))[x] # LETTRE CONCERNEE
# PLOT LETTER #
plot(-1:1, -1:1, type="n", axes=F, ann=F)
text(0, 0, lettre, cex=8, col=gray(.6))
# PLOT COUNTRIES #
sel <- countries[first == lettre, ]
sapply(order(sel$region_wb, sel$name), function(y) {
plot(sel[y, ], type="n")
plot(countries, col=gray(.7), border=gray(.6), add=T)
color = cols[[as.character(sel[y, ]$region_wb)]]
plot(sel[y, ], col=color, border=NA, add=T)
title(strwrap(iconv(sel[y, ]$name, "UTF-8", "latin1"), width=15, prefix="\n", initial=""), col.main=color, cex.main=2.5, mar=c(10,10,10,10))
})
# NULL PLOTS #
if (nullplots[x] > 0) {sapply(1:nullplots[x], function(z) plot.new())}
})
dev.off()
#################################
# CARTE DU MONDE ET SA LEGENDE #
################################
colors <- sapply(as.character(countries$region_wb), function(x) cols[[x]])
png(file="IMG/monde.png", width=3000, height=1500)
par(bg=gray(.8))
plot(countries, col=colors, border=gray(.7), lwd=1)
legend("bottomleft",
names(cols), cex=3,
fill=as.character(cols), border=NA,
bty="o", box.col=gray(.7));
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment