William Marble
February 19, 2016
The good:
The bad:
# load ggplot, install if not already installed
if (!require(ggplot2)){
install.packages("ggplot2")
require(ggplot2)
}
# and for fun themes
if (!require(ggthemes)){
install.packages("ggthemes")
require(ggthemes)
}
Congressional district-level data; unit of observation is district-year
ideol.data = read.csv(file="http://stanford.edu/~wpmarble/data/rep_data.csv")
print(head(ideol.data[, c("year", "dwnom1", "median", "gini", "party")]), digits=2)
year dwnom1 median gini party
1 1984 0.234 0.439 0.40 Republican
2 1984 0.354 0.584 0.43 Republican
3 1984 0.343 0.344 0.44 Republican
4 1984 -0.036 0.153 0.42 Democrat
5 1984 -0.202 0.318 0.42 Democrat
6 1984 -0.156 0.023 0.41 Democrat
ggplot(ideol.data, aes(x = dwnom1)) + geom_histogram()
ggplot(ideol.data, aes(x = median, y = dwnom1)) + geom_point()
ggplot(ideol.data, aes(x = median, y = dwnom1)) + geom_point() + stat_smooth(method="lm")
ggplot(ideol.data, aes(x = median, y = dwnom1)) + geom_point() + stat_smooth(method="loess")
ggplot(ideol.data, aes(x = median, y = dwnom1, colour = party)) +
geom_point() + scale_color_manual(values = c("blue", "red"))
ggplot(ideol.data, aes(x = median, y = dwnom1, colour = party)) +
geom_point(alpha = .3) + stat_smooth(method = "lm") +
scale_color_manual(values = c("blue", "red"))
ggplot(ideol.data, aes(x = median, y = dwnom1, colour = party)) +
geom_point(alpha = .3) + stat_smooth(method = "loess") +
scale_color_manual(values = c("blue", "red"))
ggplot(ideol.data, aes(x = median, y = dwnom1, colour = party)) +
geom_point(alpha = .3) + stat_smooth(method = "loess") +
scale_color_manual(name="Party", values = c("blue", "red")) +
scale_x_continuous(breaks = c(-1, 0, 1)) +
facet_grid(~decade) + labs(x = "Median donor DIME score", y = "NOMINATE score") +
ggtitle("District representation, by party and decade") +
theme_bw() + theme(legend.position = c(.1, .8), panel.grid.major = element_blank(), panel.grid.minor = element_blank())