Skip to content

@leoniedu /Unemployment.R

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
plot.heat <- function(tmp,,z,title=NULL,breaks=NULL,cex.legend=1,bw=.2,col.vec=NULL,main=NULL,plot.legend=TRUE, ...) {
tmp@data$zCat <- cut(tmp@data[,z],breaks,include.lowest=TRUE)
cutpoints <- levels(tmp@data$zCat)
if (is.null(col.vec)) col.vec <- heat.colors(length(levels(tmp@data$zCat)))
cutpointsColors <- col.vec
levels(tmp@data$zCat) <- cutpointsColors
cols <- as.character(tmp$zCat)
##cols <- "white"
plot(tmp,border=cols, lwd=bw,axes = FALSE, las = 1,col=as.character(tmp@data$zCat),main="A", ...)
if (!is.null( {
plot(,add=TRUE,lwd=1, border="white")
if (plot.legend) legend("bottomleft", cutpoints, fill = cutpointsColors,bty="n",title=title,cex=cex.legend)
##merge sp objects with data
merge.sp <- function(tmp,data,by="uf") {
by.loc <- match(by,names(data)) <- data[,by.loc]
data <- data[,-by.loc]
tmp@data <- data.frame(tmp@data,
## map data from <- readShapeSpatial("maps/st99_d00_shp/st99_d00.shp") <- readShapeSpatial("maps/co99_d00_shp/co99_d00.shp")
unemp <- read.csv("", header = F, stringsAsFactors = F)
names(unemp) <- c("id", "state_fips", "county_fips", "name", "year", "?", "?", "?", "rate")
unemp$fips <- with(unemp, paste(as.character(state_fips), as.character(county_fips), sep=";"))$state_fips <- as.numeric(as.character($STATE))$county_fips <- as.numeric(as.character($COUNTY))$fips <- with(,paste(as.character(state_fips),as.character(county_fips),sep=";"))$rate <- NULL
m <- merge.sp(, unemp, "fips")
m0 <- m
m <- m[m$state_fips%in%unemp$state_fips,]
##exclude ak
m <- m[!grepl("AK$|HI$|GU$|PR$", m$name),]
labelpos <- data.frame(, lapply(m@polygons, function(x) x@labpt)))
names(labelpos) <- c("x","y")
png(file="~/tmp.png", width=1200, height=1000)
plot.heat(m,,z="rate",breaks=c(seq(0, 10, by = 2), 35), col.vec=(brewer.pal(6,"PuRd")), xlim=c(-125,-70), ylim=c(28,48), bw=.01, plot.legend=FALSE)


I came across this page via the chloropleth challenge page. I've not yet understood all of the code above, but as you surely put a lot of effort into the challenge, I'd still like to ask you one question:
In the meantime, have you found a package that was so particularly useful for displaying (and generally working with) maps in R that you would call it your favorite? Or have you spent more time on developping your own maps-related projects?
I'm asking because I'd like to work with maps in the near future and I'm still not sure which package to use.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.