Skip to content

Instantly share code, notes, and snippets.

@pmarkun
Created April 7, 2016 18:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmarkun/1414e842b9b679e4ad618d9ac3d8a63b to your computer and use it in GitHub Desktop.
Save pmarkun/1414e842b9b679e4ad618d9ac3d8a63b to your computer and use it in GitHub Desktop.
library(rgdal)
library(dplyr)
library(ggmap)
library(RColorBrewer)
library(classInt)
gpclibPermit()
votacao <- read.csv("~/devel/eleicoes2016/raw/eleicao2012/votacao_candidato_munzona_2012_SP.csv", sep=";", quote="\"", header = FALSE, fileEncoding="cp1252")
eleitos <- votacao %>%
filter(V9 == "SÃO PAULO", V16 == "VEREADOR", V22 %in% c('ELEITO POR MÉDIA', 'ELEITO POR QP')) %>%
group_by(V15) %>%
summarise(Amount = sum(V29)) %>%
order_by(Amount)
cadeiras <- votacao %>%
filter(V9 == "SÃO PAULO", V16 == "VEREADOR") %>%
group_by(V28) %>%
summarise(Amount = sum(V29)) %>%
mutate(cadeiras = Amount/130000)
shape <- readOGR("/home/markun/devel/eleicoes2016/shape", "zonaseleitorais2")
mapeia <- function(p, votacao, shape) {
partido <- votacao
partido <- votacao %>%
filter(V16=="VEREADOR", V24==p) %>%
group_by(V10) %>%
summarise(Amount = sum(V29)) %>%
mutate(perc = Amount/sum(partido$Amount)) %>%
mutate(id = as.character(V10))
shape_ft <- fortify(shape, region="Name")
shape_ft <- left_join(shape_ft, partido, by=c('id'='id'))
#colors <- brewer.pal(9, "YlOrRd") #set breaks for the 9 colors
#brks<-classIntervals(shape_ft$Amount, n=9, style="quantile")
#brks <- brks$brks
#ggplot(shape_ft)#, col=colors[findInterval(shape_ft$Amount, brks,all.inside=TRUE)])
#c <- colors[findInterval(shape_ft$Amount, brks, all.inside=TRUE)]
ggplot(data=shape_ft, aes(x=long,y=lat, group=group, fill=shape_ft$Amount)) +
geom_polygon() +
geom_path(color='black') +
scale_fill_distiller(name="Votos", palette = "YlGn", breaks = pretty_breaks(n = 5), direction = 1) +
labs(x=NULL,y=NULL,title=p) +
coord_map()
}
v1 <- mapeia('PSOL', votacao, shape)
v2 <- mapeia('PT', votacao, shape)
v3 <- mapeia('PMDB', votacao, shape)
v4 <- mapeia('PSDB', votacao, shape)
grid.arrange(v1,v2,v3,v4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment