Created
March 1, 2013 17:39
-
-
Save chrishanretty/5066343 to your computer and use it in GitHub Desktop.
Plot municipal levels of support for Italian parties
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
## Load libraries | |
library(sp) | |
library(RColorBrewer) | |
library(car) | |
library(stringdist) | |
## Read in the shapefile | |
shape <- readOGR("../shapefiles/",layer="Comuni_11") | |
shape$NOME <- iconv(shape$NOME, "latin1", "UTF8") | |
shape$NOME.lc <- tolower(shape$NOME) | |
shape$Regione <- tolower(car:::recode(shape$COD_REG, | |
"'1'='Piemonte'; | |
'11'='Marche'; | |
'2'='Valle d\\'Aosta'; | |
'12'='Lazio'; | |
'3'='Lombardia'; | |
'13'='Abruzzo'; | |
'4'='Trentino-Alto Adige'; | |
'14'='Molise'; | |
'5'='Veneto'; | |
'15'='Campania'; | |
'6'='Friuli-Venezia Giulia'; | |
'16'='Puglia'; | |
'7'='Liguria'; | |
'17'='Basilicata'; | |
'8'='Emilia Romagna'; | |
'18'='Calabria'; | |
'9'='Toscana'; | |
'19'='Sicilia'; | |
'10'='Umbria'; | |
'20'='Sardegna'")) | |
## Load the election results | |
dat <- read.csv("2013_camera_wide.csv",header=T) | |
## Turn raw numbers into percentages | |
party.cols <- 4:19 | |
dat[,party.cols] <- dat[,party.cols] / rowSums(dat[,party.cols]) | |
## Tidy up names of municipalities | |
dat$Comune <- tolower(gsub("Comune di ","",dat$Comune)) | |
## Remove German language alternates | |
dat$Comune <- gsub("/.*","",dat$Comune) | |
## Create region variable | |
dat$Regione <- tolower(gsub(" \\d","",dat$Circoscrizione)) | |
## Strategy, within each region, create matrix of names by names | |
## pick result with smallest string distance | |
matchlists <- vector("list",length(unique(dat$Regione))) | |
unique.regions <- unique(dat$Regione) | |
for (i in 1:length(unique.regions)) { | |
b <- shape$NOME.lc [which(shape$Regione == unique.regions[i])] | |
a <- dat$Comune [which(dat$Regione == unique.regions[i])] | |
matchlists[[i]] <- stringdistmatrix(a,b) | |
rownames(matchlists[[i]]) <- a | |
colnames(matchlists[[i]]) <- b | |
} | |
## Taking each matrix in turn | |
## move down the rows (data names) | |
## and work out which shapefile name it corresponds to | |
matchvector <- vector("list",length(unique(dat$Regione))) | |
for (i in 1:length(unique.regions)) { | |
matchvector[[i]] <- data.frame(Regione = unique.regions[i], | |
dataName = rownames(matchlists[[i]]), | |
shapeName = apply(matchlists[[i]],1,function(x)colnames(matchlists[[i]])[which.min(x)]), | |
distance = apply(matchlists[[i]],1,min)) | |
} | |
matchlist <- do.call("rbind",matchvector) | |
## There's a tiny problem with one entry | |
matchlist$shapeName [which(matchlist$dataName=="gravedona ed uniti")] <- NA | |
## Now merge | |
dat <- merge(dat,matchlist, | |
by.x=c("Regione","Comune"), | |
by.y=c("Regione","dataName"), | |
all.x=T,all.y=F) | |
## Merge again, using region and shapefile name as keys | |
ita <- merge(shape,dat, | |
by.x=c("Regione","NOME.lc"), | |
by.y=c("Regione","shapeName"), | |
all.x=T,all.y=F,sort=FALSE) | |
## ensure same order as shapefile | |
shape$sortname <- paste0(shape$Regione,shape$NOME.lc) | |
ita$sortname <- paste0(ita$Regione,ita$NOME.lc) | |
my.order <- match(shape$sortname,ita$sortname) | |
ita <- ita[my.order,] | |
table(ita$sortname==shape$sortname) | |
## Set up the information for the plots | |
party.cols <- 14:28 | |
names(ita)[party.cols] | |
party.palettes <- recode(names(ita)[party.cols], | |
"'Centro.Democratico'='OrRd'; | |
'Fare.per.Fermare.il.Declino'='Greys'; | |
'Fratelli.d.Italia'='PuBu'; | |
'Futuro.e.libertà..Fli.'='GnBu'; | |
'Grande.Sud...Mpa'='PuBuGn'; | |
'Il.Popolo.della.libertà..Pdl.'='Blues'; | |
'La.Destra'='Greys'; | |
'Lega.Nord'='Greens'; | |
'MoVimento.5.Stelle...beppegrillo.it'='Purples'; | |
'Partito.Democratico..Pd.'='Reds'; | |
'Rivoluzione.Civile'='Oranges'; | |
'Scelta.Civica.con.Monti.per.l.Italia'='YlGnBu'; | |
'Sinistra.ecologia.e.libertà..Sel.'='OrRd'; | |
'Südtiroler.Volkspartei..Svp.'='YlGn'; | |
'Unione.di.centro..Udc.'='Blues'" | |
) | |
## Iterate over parties | |
for (the.party in party.cols) { | |
party.name <- names(ita)[the.party] | |
party.name <- gsub("\\."," ",party.name) | |
my.breaks <- quantile(ita[,the.party],probs=seq(0,1,by=1/9),na.rm=T) | |
## For parties with concentrated support, take only non-zero elements | |
if (any(duplicated(my.breaks))) { | |
tmp <- ita[,the.party] | |
tmp <- tmp[tmp>0] | |
my.breaks <- quantile(tmp,probs=seq(0,1,by=1/9),na.rm=T) | |
} | |
ita$cutcolors <- cut(as.numeric(ita[,the.party]), my.breaks,na.rm=T) | |
shape$cutcolors <- ita$cutcolors | |
myPalette <- brewer.pal(9, party.palettes[which(party.cols==the.party)]) | |
outfile <- paste0("graphics/",party.name,"_map.pdf") | |
outfile <- gsub(" ","_",outfile) | |
plot.title <- paste0(party.name," vote share by comune") | |
pdf(file=outfile,width=21.0/2.54,height=29.7/2.54,paper="a4") | |
print(spplot(shape, "cutcolors", col="transparent", col.regions=myPalette, main=plot.title)) | |
dev.off() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment