Data preparation
# http://flowingdata.com/2009/11/12/how-to-make-a-us-county-thematic-map-using-free-tools/
# http://blog.revolutionanalytics.com/2009/11/choropleth-challenge-result.html
# Data manipulation from
# https://gist.github.com/hadley/233134
unemp <- read.csv("http://datasets.flowingdata.com/unemployment09.csv",
header = FALSE, stringsAsFactors = FALSE)
names(unemp) <- c("id", "state_fips", "county_fips", "name", "year",
"?", "?", "?", "rate")
unemp$county <- tolower(gsub(" County, [A-Z]{2}", "", unemp$name))
unemp$county <- gsub("^(.*) parish, ..$","\\1", unemp$county)
unemp$state <- gsub("^.*([A-Z]{2}).*$", "\\1", unemp$name)
unemp$statename <- tolower(state.name[match(unemp$state, state.abb)])
unemp$stateco <- paste(unemp$statename, unemp$county, sep=",")
head(unemp)
## id state_fips county_fips name year ? ? ? rate
## 1 CN010010 1 1 Autauga County, AL 2009 23,288 21,025 2,263 9.7
## 2 PA011000 1 3 Baldwin County, AL 2009 81,706 74,238 7,468 9.1
## 3 CN010050 1 5 Barbour County, AL 2009 9,703 8,401 1,302 13.4
## 4 CN010070 1 7 Bibb County, AL 2009 8,475 7,453 1,022 12.1
## 5 CN010090 1 9 Blount County, AL 2009 25,306 22,789 2,517 9.9
## 6 CN010110 1 11 Bullock County, AL 2009 3,527 2,948 579 16.4
## county state statename stateco
## 1 autauga AL alabama alabama,autauga
## 2 baldwin AL alabama alabama,baldwin
## 3 barbour AL alabama alabama,barbour
## 4 bibb AL alabama alabama,bibb
## 5 blount AL alabama alabama,blount
## 6 bullock AL alabama alabama,bullock
Comparing colormaps
# Now, latticeExtra::mapplot
require(pals) # colormaps
## Loading required package: pals
## Loading required package: maps
##
## Attaching package: 'maps'
## The following object is masked _by_ '.GlobalEnv':
##
## unemp
require(latticeExtra) # for mapplot
## Loading required package: latticeExtra
## Loading required package: lattice
## Loading required package: RColorBrewer
require(maps)
data(county.fips)
# very poor and much darker than ggplot
# The extreme range of viridis makes it hard to find a contrasting border color
mapplot(stateco ~ rate , data=unemp,
colramp=viridis, #breaks=c(seq(0, 10, by = 2), 35),
border="gray85",
main="viridis", xlab="",
scales=list(draw=FALSE),
map=map('county', plot=FALSE, fill=TRUE, projection="tetra"))
## Warning in (function (x, y, map, breaks, colramp, exact = FALSE, lwd = 0.5, : 199
## unmatched regions: alabama,dekalb, alabama,st. clair, alaska,aleutians east....

revp <- function(pal, n=100){
colorRampPalette(rev(pal(n)))
}
rev.function <- function(x, n=100) colorRampPalette(rev(x(n)))
# Flowing Data
mapplot(stateco ~ rate , data=unemp,
colramp=brewer.purd, breaks=c(seq(0, 10, by = 2), 35),
border="gray85",
main="viridis", xlab="",
scales=list(draw=FALSE),
map=map('county', plot=FALSE, fill=TRUE, projection="tetra"))
## Warning in (function (x, y, map, breaks, colramp, exact = FALSE, lwd = 0.5, : 199
## unmatched regions: alabama,dekalb, alabama,st. clair, alaska,aleutians east....

mapplot(stateco ~ rate , data=unemp,
colramp=revp(parula), breaks=c(seq(0, 10, by = 2), 35),
#border="gray85",
main="parula", xlab="",
scales=list(draw=FALSE),
map=map('county', plot=FALSE, fill=TRUE, projection="tetra"))
## Warning in (function (x, y, map, breaks, colramp, exact = FALSE, lwd = 0.5, : 199
## unmatched regions: alabama,dekalb, alabama,st. clair, alaska,aleutians east....

# To my eye, green and blue are much more pronounced than purple
mapplot(stateco ~ rate , data=unemp,
colramp=cubicyf,
main="cubicyf", xlab="",
scales=list(draw=FALSE),
map=map('county', plot=FALSE, fill=TRUE, projection="tetra"))
## Warning in (function (x, y, map, breaks, colramp, exact = FALSE, lwd = 0.5, : 199
## unmatched regions: alabama,dekalb, alabama,st. clair, alaska,aleutians east....

# More color, including orange at top
mapplot(stateco ~ rate , data=unemp,
colramp=cubicl,
main="cubicl", xlab="",
scales=list(draw=FALSE),
map=map('county', plot=FALSE, fill=TRUE, projection="tetra"))
## Warning in (function (x, y, map, breaks, colramp, exact = FALSE, lwd = 0.5, : 199
## unmatched regions: alabama,dekalb, alabama,st. clair, alaska,aleutians east....

# too dark
mapplot(stateco ~ rate , data=unemp,
colramp=ocean.haline,
main="ocean.haline", xlab="",
scales=list(draw=FALSE),
map=map('county', plot=FALSE, fill=TRUE, projection="tetra"))
## Warning in (function (x, y, map, breaks, colramp, exact = FALSE, lwd = 0.5, : 199
## unmatched regions: alabama,dekalb, alabama,st. clair, alaska,aleutians east....

# Good
mapplot(stateco ~ rate , data=unemp,
colramp=colorRampPalette(rev(brewer.gnbu(30))),
main="brewer.gnbu", xlab="",
scales=list(draw=FALSE),
map=map('county', plot=FALSE, fill=TRUE, projection="tetra"))
## Warning in (function (x, y, map, breaks, colramp, exact = FALSE, lwd = 0.5, : 199
## unmatched regions: alabama,dekalb, alabama,st. clair, alaska,aleutians east....

# Too dark
mapplot(stateco ~ rate , data=unemp,
colramp=colorRampPalette(rev(brewer.ylgnbu(30))),
main="brewer.ylgnbu", xlab="",
scales=list(draw=FALSE),
map=map('county', plot=FALSE, fill=TRUE, projection="tetra"))
## Warning in (function (x, y, map, breaks, colramp, exact = FALSE, lwd = 0.5, : 199
## unmatched regions: alabama,dekalb, alabama,st. clair, alaska,aleutians east....
