Skip to content

Instantly share code, notes, and snippets.

@rjpower
Created March 20, 2012 22:02
Show Gist options
  • Save rjpower/2141739 to your computer and use it in GitHub Desktop.
Save rjpower/2141739 to your computer and use it in GitHub Desktop.
ggplot2 using census data
require("maps")
require("ggplot2")
require("sp")
census_data = "ACS_10_1YR_S1901/ACS_10_1YR_S1901_with_ann.csv"
t = read.csv(census_data, skip=6)
sinfo = data.frame(region=t$Geography,
mean=t$Estimate.48,
median=t$Estimate.44,
discrep=(t$Estimate.48 / t$Estimate.44) - 1)
sinfo$k10 = ctable$Estimate.4
sinfo$k15 = ctable$Estimate.8
sinfo$k25 = ctable$Estimate.12
sinfo$k35 = ctable$Estimate.16
sinfo$k50 = ctable$Estimate.20
sinfo$k75 = ctable$Estimate.24
sinfo$k100 = ctable$Estimate.28
sinfo$k150 = ctable$Estimate.32
sinfo$k200 = ctable$Estimate.36
sinfo$k1000 = ctable$Estimate.40
sinfo$region = casefold(sinfo$region)
sinfo$union = 0
ustates = c(
"california", "colorado", "connecticut", "delaware", "hawaii", "illinois", "iowa",
"maine", "maryland", "massachusetts", "nevada", "new hampshire", "new york",
"oregon", "rhode island", "vermont", "washington", "wisconsin")
sinfo$union[sinfo$region %in% ustates] = 1
state_centers = data.frame(
region=casefold(state.name),
center=state.center)
m3 <- merge(map_data('state'), sinfo, by='region', all=T)
m3 <- merge(m3, state_centers, by='region', all=T)
m3 <- m3[order(m3$order),]
m3$union = as.factor(m3$union)
p <-ggplot(m3, aes(x=long, y=lat, group=group, fill=k100 + k150)) +
geom_polygon() +
geom_point(aes(x=center.x, y=center.y, color=union)) +
scale_fill_gradient(low="red", high="darkgreen") +
labs(x="", y="", fill="Income");
d=data.frame(region=sinfo$region,income=sinfo$net)
d$r2 = factor(d$region, levels = d$region[order(d$income)])
d$union = sinfo$union
p <- qplot(income, r2, data=d, color=union)
show(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment