Skip to content

Instantly share code, notes, and snippets.

@diegovalle
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save diegovalle/6b607ba30f5b4aa75895 to your computer and use it in GitHub Desktop.
Save diegovalle/6b607ba30f5b4aa75895 to your computer and use it in GitHub Desktop.
2015 Election
library(ggplot2)
library(dplyr)
library(readr)
library(rgeos)
library(maptools)
library(rgdal)
library(scales)
library(downloader)
#Download the latest version of the PREP
temp <- tempfile()
download.file("http://prep2015.ine.mx/20150608_2010-listaActas.tar.gz",temp)
untar(temp, "./diputados.csv")
dip = read_delim("./diputados.csv", delim = "|", skip = 7,
col_names = c("ID_ESTADO","ESTADO","ID_DISTRITO","DISTRITO",
"SECCION","ID_CASILLA","TIPO_CASILLA","EXT_CONTIGUA",
"UBICACION_CASILLA","TIPO_ACTA","NUM_BOLETAS_SOBRANTES",
"TOTAL_CIUDADANOS_VOTARON","NUM_BOLETAS_EXTRAIDAS","PAN",
"PRI","PRD","PVEM","PT","MOVIMIENTO_CIUDADANO",
"NUEVA_ALIANZA","MORENA","PARTIDO_HUMANISTA",
"ENCUENTRO_SOCIAL","PRI_PVEM","PRD_PT","CAND_IND_1",
"CAND_IND_2","NO_REGISTRADOS","NULOS","TOTAL_VOTOS",
"LISTA_NOMINAL","OBSERVACIONES","CONTABILIZADA","CRYT",
"SHA","HORA_ACOPIO","HORA_CAPTURA","HORA_REGISTRO",
"null"))
unlink(temp)
#download the hex grid map
download("https://gist.githubusercontent.com/diegovalle/7d198d31c53adbbc8597/raw/25e301eaecace2123a4a38c54d9a3dd58318a854/mx_hexgrid.json",
"mx_hexgrid.json")
map <- readOGR("mx_hexgrid.json", "OGRGeoJSON")
#state abbreviations
centers <- coordinates(map)
centers <- cbind(centers, map@data)
names(centers)[1:3] <- c("lat", "long", "id")
#sum(as.numeric(dip$TOTAL_VOTOS), na.rm = TRUE) - sum(as.numeric(dip$NO_REGISTRADOS), na.rm = TRUE)- sum(as.numeric(dip$NULOS), na.rm = TRUE)
#sum(as.numeric(dip$NULOS), na.rm = TRUE)- sum(as.numeric(dip$NO_REGISTRADOS), na.rm = TRUE)
#sum(as.numeric(dip$PAN), na.rm = TRUE)
#Create the data.frame for making the map
createDF <- function(dip, party) {
dip$temp <- dip[[party]]
dip %>%
group_by(ESTADO, ID_ESTADO) %>%
summarise(per = sum(as.numeric(temp), na.rm = TRUE) /
(sum(as.numeric(TOTAL_VOTOS), na.rm = TRUE) -
sum(as.numeric(NULOS), na.rm = TRUE) -
sum(as.numeric(NO_REGISTRADOS), na.rm = TRUE))) %>%
rename(state_code = ID_ESTADO)
}
#Hex Grid Map
mapStates <- function(map, party, low = "gray90", high, title = "") {
map@data <- left_join(map@data, party)
mx_map <- fortify(map, region="state_abbr")
ggplot() +
geom_map(map=mx_map, data = mx_map, color="black",
aes(map_id=id, x = long, y = lat)) +
geom_map(map=mx_map, data = map@data, color="white",
aes(map_id = state_abbr, fill = per)) +
coord_map() +
geom_text(data = centers,
aes(label=id, x=lat, y=long), color="white", size=4.5) +
scale_fill_gradient("porcentaje", low = low, high = high, labels = percent) +
theme_bw() +
ggtitle(title) +
theme(panel.border=element_blank()) +
theme(panel.grid=element_blank()) +
theme(axis.ticks=element_blank()) +
theme(axis.text=element_blank()) +
labs(x=NULL, y=NULL)
}
PAN <- createDF(dip, "PAN")
PRD <- createDF(dip, "PRD")
PRI <- createDF(dip, "PRI")
MORENA <- createDF(dip, "MORENA")
PVEM <- createDF(dip, "PVEM")
MC <- createDF(dip, "MOVIMIENTO_CIUDADANO")
N_A <- createDF(dip, "NUEVA_ALIANZA")
PT <- createDF(dip, "PT")
PH <- createDF(dip, "PARTIDO_HUMANISTA")
ES <- createDF(dip, "ENCUENTRO_SOCIAL")
mapStates(map, PRI, high = "#ec242a", title = "Porcentaje PRI")
mapStates(map, PVEM, high = "#2ca25f", title = "Porcentaje PVEM")
mapStates(map, PAN, high = "#02569b", title = "Porcentaje PAN")
mapStates(map, PRD, high = "#fec44f", title = "Porcentaje PRD")
mapStates(map, MORENA, high = "#d95f0e", title = "Porcentaje MORENA")
mapStates(map, MC, high = "#f58e1e", title = "Porcentaje Moviemiento Ciudadano")
mapStates(map, N_A, high = "#37b4b7", title = "Porcentaje Nueva Alianza")
mapStates(map, PT, high = "#e8122e", title = "Porcentaje PT")
mapStates(map, PH, high = "#a7448b", title = "Porcentaje Partido Humanista")
mapStates(map, ES, high = "#643179", title = "Porcentaje Encuentro Social")
#Voto MORENA como porcentaje del voto PRD
MPRD <- dip %>%
group_by(ESTADO, ID_ESTADO) %>%
summarise(per = sum(as.numeric(MORENA), na.rm = TRUE) /
sum(as.numeric(PRD), na.rm = TRUE)) %>%
rename(state_code = ID_ESTADO)
mapStates(map, MPRD, low = "#d8b365", high = "#5ab4ac",
title = "Voto MORENA como porcentaje del voto PRD") +
scale_fill_gradient2(high = "#991b06", low = "#f7cf00", midpoint = 1, mid = "#f5f5f5",
labels = percent)
@Crris
Copy link

Crris commented Jun 16, 2015

Hi Diego
I have troubles just in the last section

mapStates(map, PRI, high = "#ec242a", title = "Porcentaje PRI")
Joining by: "state_code"
Error: isTRUE(gpclibPermitStatus()) is not TRUE
Called from: stop(sprintf(ngettext(length(r), "%s is not TRUE", "%s are not all TRUE"),
ch), call. = FALSE, domain = NA)
Browse[1]>

Can you help me?

@Crris
Copy link

Crris commented Jun 16, 2015

Thanks for your answer in Twitter

I had to install the libraries "rgeos" and "maptools"
and I had to change the line 12
download.file("http://prep2015.notimex.gob.mx/20150608_1922-listaActas.tar.gz",temp)
by
download.file("http://prep2015.ine.mx/20150608_2010-listaActas.tar.gz",temp)

Thank U

@diegovalle
Copy link
Author

Should be working now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment