Create a gist now

Instantly share code, notes, and snippets.

@rCarto /brexit.R
Last active Dec 15, 2016

library(sp)
library(raster)
library(rgeos)
## import des données
download.file(url = "http://biogeo.ucdavis.edu/data/gadm2.8/rds/GBR_adm2.rds",
destfile = file.path(tempdir(), "GBR_adm2.rds"))
download.file(url = "http://biogeo.ucdavis.edu/data/gadm2.8/rds/IRL_adm0.rds",
file.path(tempdir(),"IRL_adm0.rds"))
download.file(url = "http://biogeo.ucdavis.edu/data/gadm2.8/rds/FRA_adm0.rds",
file.path(tempdir(),"FRA_adm0.rds"))
UK <- readRDS(file.path(tempdir(),"GBR_adm2.rds"))
IRL <- readRDS(file.path(tempdir(),"IRL_adm0.rds"))
FR <- readRDS(file.path(tempdir(),"FRA_adm0.rds"))
loc <- "https://f.hypotheses.org/wp-content/blogs.dir/253/files/2016/12/EU-referendum-result-data.csv"
referendum <-read.csv(loc,header=TRUE,dec=".",sep=",",stringsAsFactors = FALSE)
## Manipulation/transformation des données
# correction d'un code
UK[159,"HASC_2"]="GB.NR"
# changement de projection
UK <- spTransform(UK, CRSobj = "+init=epsg:3035")
IRL <- spTransform(IRL, CRSobj = "+init=epsg:3035")
FR <- spTransform(FR, CRSobj = "+init=epsg:3035")
# agrégation des unités d'Irlande du Nord
UK[UK$NAME_1=="Northern Ireland", "HASC_2" ] <- "Northern Ireland"
UK <- rgeos::gUnaryUnion(spgeom = UK, id = UK$HASC_2)
ids <- sapply(methods::slot(UK, "polygons"),methods::slot, "ID")
UK <- SpatialPolygonsDataFrame(Sr = UK,
data = data.frame(id = ids, row.names = ids))
# agrégation des données du referendum
referendum <-referendum[c(3,6,13,14)]
referendum <- aggregate(referendum[, 3:4],
by = list(HASC_code = referendum$HASC_code,
Region = referendum$Region),
FUN = sum)
referendum <- referendum[referendum$HASC_code != "Gibraltar", ]
referendum[referendum$Remain>referendum$Leave, "brexit"] <- "Remain"
referendum[referendum$Remain<referendum$Leave, "brexit"] <- "Leave"
referendum$nvote <- referendum$Leave + referendum$Remain
referendum$shareLeave <- 100 * referendum$Leave / referendum$nvote
library(cartography)
png("majo.png", width = 450, height = 600, res = 100)
par(mar = c(0,0,1.2,0))
plot(UK, ylim = c(3100000, 4100000), xlim = c(2950000, 3750000),
col = NA, border = NA, bg = "#A6CAE0")
plot(IRL, col = "lightgrey", border = NA, add=TRUE)
plot(FR, col = "lightgrey", border = NA, add=TRUE)
typoLayer(spdf = UK, spdfid = "id",
df = referendum, dfid = "HASC_code",
var = "brexit",
col = c("#5A9C50", "#EC4E49"),
legend.pos = 'topleft',
legend.title.txt = "",
add = TRUE)
layoutLayer(title = "Vote majoritaire au référendum sur le brexit",
author = "Données: The Electoral Comission (https://frama.link/ElectoralCommissionUK)",
sources = "Fond de carte: GADM 2.8 (http://gadm.org/)",
theme = "blue.pal", scale = 100)
dev.off()
png("shareleave.png", width = 450, height = 600, res = 100)
par(mar = c(0,0,1.2,0))
plot(UK, ylim = c(3100000, 4100000), xlim = c(2950000, 3750000),
col = NA, border = NA, bg = "#A6CAE0")
plot(IRL, col = "lightgrey", border = NA, add=TRUE)
plot(FR, col = "lightgrey", border = NA, add=TRUE)
# limites des classes
bks <- c(min(referendum$shareLeave),
seq(30,70,10),
max(referendum$shareLeave))
# palette de couleur
cols <- carto.pal(pal1 = "green.pal", n1 = 3,
pal2 = "red.pal", n2 = 3)
choroLayer(spdf = UK, spdfid = "id",
df = referendum, dfid = "HASC_code",
var = "shareLeave", breaks = bks,
col = cols,
legend.pos = 'topleft',
legend.title.txt = "Part du vote 'leave'\n (en %)",
add = TRUE)
layoutLayer(title = "Part du 'Leave' au référendum sur le brexit",
author = "Données: The Electoral Comission (https://frama.link/ElectoralCommissionUK)",
sources = "Fond de carte: GADM 2.8 (http://gadm.org/)",
theme = "blue.pal", scale = 100)
dev.off()
png("shareleaveprop.png", width = 450, height = 600, res = 100)
par(mar = c(0,0,1.2,0))
plot(UK, ylim = c(3100000, 4100000), xlim = c(2950000, 3750000),
col = "grey50", border = "grey80",lwd = 0.5, bg = "#A6CAE0")
plot(IRL, col = "lightgrey", border = NA, add=TRUE)
plot(FR, col = "lightgrey", border = NA, add=TRUE)
propSymbolsChoroLayer(spdf = UK, spdfid = "id",
df = referendum, dfid = "HASC_code",
var = "nvote",inches = 0.2,
var2 = 'shareLeave',
breaks = bks,
col = cols)
layoutLayer(title = "Répartition et part du 'Leave' au référendum sur le brexit",
author = "Données: The Electoral Comission (https://frama.link/ElectoralCommissionUK)",
sources = "Fond de carte: GADM 2.8 (http://gadm.org/)",
theme = "blue.pal", scale = 100)
text(x = 3018138, y = 3255515,
font = 2, srt = 33, cex = 1, pos = 4,
labels = "La carte n'est pas lisible\ndans la région de Londres")
dev.off()
# library(devtools)
# install_github('riatelab/mapinsetr')
library(mapinsetr)
# définition d'une bounding box autour de la région de Londres
bb <- matrix(data = c(3598988, 3648341, 3177688, 3221347),
nrow = 2, ncol = , byrow = T )
# création d'un masque destiné à extraire les unités londoniennes
londonMask <- create_mask(bb = bb,
add = FALSE,
prj = UK@proj4string)
# déplacement et redimensionnement des unités
zoomLondon <- move_and_resize(spdf = UK,
mask = londonMask,
xy = c(3001055, 3196670),
k = 4)
# déplacement et redimensionnement du masque
zoomBox <- move_and_resize(spdf = londonMask,
mask = londonMask,
xy = c(3001055, 3196670),
k = 4)
# fusion du fond de carte initial et du zoom
UK2 <- inset_rbinder(list(UK, zoomLondon))
# affichage du fond de carte
png("UK2.png", width = 450, height = 600, res = 100)
par(mar = c(0,0,1.2,0))
plot(UK2)
layoutLayer("Fond de carte UK avec zoom sur Londres", scale = NULL,
north = FALSE, sources = "", author = "")
dev.off()
png("shareleaveprop2.png", width = 450, height = 600, res = 100)
par(mar = c(0,0,1.2,0))
plot(FR, ylim = c(3100000, 4100000), xlim = c(2950000, 3750000),
col = 'lightgrey', border = NA, bg = "#A6CAE0")
plot(IRL, col = "lightgrey", border = NA, add=TRUE)
plot(UK2, col = "grey50", border = "grey80",lwd = 0.5, add=TRUE)
plot(zoomBox, lwd = 1, add=T)
text(x = 3001055, y = 3196670, labels = "Zoom sur Londres",
adj = c(0,1.5), cex = 0.7 )
propSymbolsChoroLayer(spdf = UK2, spdfid = "id",
df = referendum, dfid = "HASC_code",
var = "nvote",inches = 0.2,
legend.var.title.txt = "Nombre de\nvotes exprimés",
legend.var.pos = "topleft",
var2 = 'shareLeave',
legend.var2.pos = "left",
legend.var2.values.rnd = 0,
legend.var2.title.txt = "Part du vote\n'Leave' (en %)",
breaks = bks,
col = cols)
plot(londonMask, lwd = 1, add=T)
layoutLayer(title = "Répartition et part du 'Leave' au référendum sur le brexit",
author = "Données: The Electoral Comission (https://frama.link/ElectoralCommissionUK)",
sources = "Fond de carte: GADM 2.8 (http://gadm.org/)",
theme = "blue.pal", scale = 100)
dev.off()