Skip to content

Instantly share code, notes, and snippets.

@sotelo
Created June 23, 2014 18:47
Show Gist options
  • Save sotelo/1dce06d3fdd61cbb95b8 to your computer and use it in GitHub Desktop.
Save sotelo/1dce06d3fdd61cbb95b8 to your computer and use it in GitHub Desktop.
Municipios de Mexico con datos de INEGI.
# http://mapserver.inegi.org.mx/MGN/mge2010v5_0a.zip
setwd("d:/User Profiles/G13087/Documents/R/Mapas/INEGI/Municipios")
rm(list = ls())
require("rgdal")
require("maptools")
require("ggplot2")
require("plyr")
require("rgeos")
f <- function(n)
{
cve = paste("00", n, sep = "")
ln = nchar(cve)
cve = substring(cve, ln-1,ln)
mex = readOGR(dsn=".", layer="MUNICIPIOS")
mex = mex[mex$CVE_ENT == cve,]
mex@data$id = rownames(mex@data)
mex.points = fortify(mex, region="id")
mex.df = join(mex.points, mex@data, by="id")
Datos <- data.frame(id = sort(unique(mex.df$id)), val = as.factor(sample.int(3,length(unique(mex.df$id)), replace = T)))
M <- ggplot(Datos, aes(map_id = id))
M <- M + geom_map(aes(fill = val), map = mex.df)
M <- M + expand_limits(x = mex.df$long, y = mex.df$lat)
M <- M + geom_path(data = mex.df, aes(long,lat,group=group), color = "white" )
M <- M <- M + coord_fixed()
M <- ggplot(Datos, aes(map_id = id))
M <- M + geom_map(aes(fill = val), map = mex.df)
M <- M + expand_limits(x = mex.df$long, y = mex.df$lat)
M <- M + geom_path(data = mex.df, aes(long,lat,group=group), color = "white" )
M <- M + coord_fixed()
M <- M + theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank())
return(M)
}
lapply(27:32, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment