x <- c("dplyr", "ggmap", "ggplot2", "RColorBrewer", "rgdal", "classInt", "RCurl", "grid","gridExtra")
lapply(x, library, character.only = TRUE)
no_ylab = ylab("")
no_xlab = xlab("")
plain_theme = theme(axis.text=element_blank()) + theme(panel.background = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank())
remove.farflung = function(.df) {
subset(.df,
.df$id != "AS" &
.df$id != "MP" &
.df$id != "GU" &
.df$id != "PR" &
.df$id != "VI" &
.df$id != "AK" &
.df$id != "HI"
)
}
intervals = function(.df, ...){
argList = match.call(expand.dots=FALSE)$...
for(i in 1:length(argList)){
colName <- argList[[i]]
series_colName = eval(substitute(colName), envir=.df, enclos=parent.frame())
min <- min(series_colName)
max <- max(series_colName)
diff <- max - min
std <- sd(series_colName)
equal.interval <- seq(min, max, by = diff/6)
quantile.interval <- quantile(series_colName, probs = seq(0, 1, by = 1/6))
std.interval <- c(seq(min, max, by = std), max)
natural.interval <- classIntervals(series_colName, n = 6, style = 'jenks')$brks
.df$equal <- cut(series_colName, breaks = equal.interval, include.lowest = TRUE)
names(.df)[names(.df)=="equal"] <- paste(colName,".","equal", sep = '')
.df$quantile <- cut(series_colName, breaks = quantile.interval, include.lowest = TRUE)
names(.df)[names(.df)=="quantile"] <- paste(colName,".","quantile", sep = '')
.df$std <- cut(series_colName, breaks = std.interval, include.lowest = TRUE)
names(.df)[names(.df)=="std"] <- paste(colName,".","std", sep = '')
.df$natural <- cut(series_colName, breaks = natural.interval, include.lowest = TRUE)
names(.df)[names(.df)=="natural"] <- paste(colName,".","natural", sep = '')
}
return(.df)
}
setwd("/Users/rc/Desktop/gitmo/playpen/r/") # Your directory here
dsn <- "data/cb_2014_us_state_5m"
layer <- "cb_2014_us_state_5m"
cb5 = readOGR(dsn, layer)
us = fortify(cb5, region = "STUSPS")
population<- read.csv(textConnection(getURL('https://www.census.gov/popest/data/national/totals/2014/files/NST_EST2014_ALLDATA.csv')))[-(1:5),]
pop <- data.frame(population$STATE,population$POPESTIMATE2014)
names(pop) = c("fips", "pop")
converter = read.csv("csv/state_fips_postal.csv", header = FALSE)
converter = rename(converter, state = V1, fips = V2, id = V3)
pop = merge(pop, converter)
pop$state = NULL
pop$fips = NULL
pop48 = remove.farflung(pop)
pop48 = intervals(pop48, pop)
us48 = remove.farflung(us)
map48 = merge(us48,pop48) # has to be geo first
my_title = "Map of 48 Contiguous States and District of Columbia, default settings"
sources <- "Source: U.S. Census Bureau 2014 Cartographic Boundary File (States, cb_2014_us_state_5m) http://1.usa.gov/1IZgFLV\nPrepared by: @technocrat 2015-07-15"
b = ggplot(data = map48, aes(x=long, y=lat, group = group))
d = b + geom_polygon(aes(x=long, y=lat, group=group), color = "dark grey", size = 0.3) + ggtitle(my_title) + annotate("text", x = -110, y = 18, label = sources, size = 3, family = "Times", colour = "black")
d
my_title = "Map of 48 Contiguous States and District of Columbia, white background"
w = d + geom_polygon(aes(x=long, y=lat, group=group), color = "dark grey", size = 0.3, fill="white") + ggtitle(my_title) + annotate("text", x = -110, y = 18, label = sources, size = 3, family = "Times", colour = "black")
w
my_title = "Population Map of 48 Contiguous States and District of Columbia, default settings"
sources <- "Source: U.S. Census Bureau 2014 Population Estimates http://1.usa.gov/1LPM7hc\n and Cartographic Boundary File (States, cb_2014_us_state_5m) http://1.usa.gov/1IZgFLV\nPrepared by: @technocrat 2015-07-15"
p = b + geom_polygon(color = "dark grey", size = 0.3, aes(fill = pop)) + ggtitle(my_title) + annotate("text", x = -110, y = 18, label = sources, size = 3, family = "Times", colour = "black") # blues, reversed
p
my_title = "Population Map of 48 Contiguous States and District of Columbia,\n no background or axis legends, polyconic projection"
p + plain_theme + no_ylab + no_xlab + coord_map("polyconic") + ggtitle(my_title) + annotate("text", x = -110, y = 18, label = sources, size = 3, family = "Times", colour = "black") # blues, reversed
my_title = "Equal Interval Scale"
e = b + geom_polygon(aes(x=long, y=lat, group=group, fill=pop.equal), color = "dark grey", size = 0.3)
e = e +
plain_theme + no_ylab + no_xlab + coord_map("polyconic") + scale_fill_brewer(palette = "YlOrBr") + ggtitle(my_title)
my_title = "Quantile Interval Scale"
q = b + geom_polygon(aes(x=long, y=lat, group=group, fill=pop.quantile), color = "dark grey", size = 0.3)
q = q +
plain_theme + no_ylab + no_xlab + coord_map("polyconic") + scale_fill_brewer(palette = "YlOrBr") + ggtitle(my_title)
my_title = "Standard Deviation Interval Scale"
s = b + geom_polygon(aes(x=long, y=lat, group=group, fill=pop.std), color = "dark grey", size = 0.3)
s = s + plain_theme + no_ylab + no_xlab + coord_map("polyconic") + scale_fill_brewer(palette = "YlOrBr") + ggtitle(my_title)
my_title = "Natural Interval Scale"
n = b + geom_polygon(aes(x=long, y=lat, group=group, fill=pop.natural), color = "dark grey", size = 0.3)
n = n + plain_theme + no_ylab + no_xlab + coord_map("polyconic") + scale_fill_brewer(palette = "YlOrBr") + ggtitle(my_title)
grid.arrange(e, q, s, n, ncol = 2)