Skip to content

Instantly share code, notes, and snippets.

@eaglebh
Created July 15, 2014 10:35
Show Gist options
  • Save eaglebh/31810ea0470784746c71 to your computer and use it in GitHub Desktop.
Save eaglebh/31810ea0470784746c71 to your computer and use it in GitHub Desktop.
mapa <- readShapeSpatial('Brasil.shp')
plot(mapa)
summary(mapa)
y <- read.csv('prefeitos12.txt')
x <- attr(mapa,"data")
x
x$indice <- 1:dim(x)[1]
x
dados <- merge(x, y, by = "UF")
dados
dados <- dados[order(dados$indice), ] #ordenar
dados
intervalos <- c(-Inf,5,10,15,20,Inf) # este objeto vai cortar as porcentagens nos valores informados
intervalos
cortes <- cut((dados$PT/dados$tMUN)*100, intervalos, include.lowest=TRUE) #cortei as % em 5 níveis
cortes
(niveis <- levels(cortes))
cores <- palette(c("#F1FF6D", "#FFE659", "#FFA428", "#FE7315", "#F8561D"))
cores
levels(cortes) <- cores
levels
cortes
plot(mapa, border=gray(0.9), lwd=.1, axes = FALSE, las = 1, col=as.character(cortes))
pct <-round(((dados$PT/dados$tMUN)*100), digits = 1)
text(get.Poly(brasil), labels=as.character(pct), cex=.8)
title("% de Prefeitos Eleitos em 2012: PT")
plot(brasil,add=TRUE,lwd=.5)
pct <-round(((dados$PT/dados$tMUN)*100), digits = 1)
text(get.Poly(mapa), labels=as.character(pct), cex=.8)
title("% de Prefeitos Eleitos em 2012: PT")
plot(mapa,add=TRUE,lwd=.5)
??Poly
??get.Poly
get.Poly<-function(x)
{
Srs<-slot(mapa,"polygons")
t(sapply(Srs, function(i) slot(i,'labpt')))
}
pct <-round(((dados$PT/dados$tMUN)*100), digits = 1)
text(get.Poly(mapa), labels=as.character(pct), cex=.8)
title("% de Prefeitos Eleitos em 2012: PT")
plot(mapa,add=TRUE,lwd=.5)
cores <- heat.colors(length(levels(cortes)))
levels(cortes) <- cores
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment