Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Last active May 20, 2019 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/7037aabf1257f6e9d8ebc00cdc6fab3f to your computer and use it in GitHub Desktop.
Save ryanburge/7037aabf1257f6e9d8ebc00cdc6fab3f to your computer and use it in GitHub Desktop.
Freethought Election
## Hex Map of Nones ####
cces18 <- read_dta("D://cces18.dta")
www <- cces18 %>%
mutate(nones = car::recode(religpew, "9:11=1; else =0")) %>%
mutate(state = to_factor(inputstate)) %>%
group_by(state) %>%
mean_ci(nones, wt = commonweight)
library(geogrid)
library(raster)
US_shp <- rgdal::readOGR("D:\\cb_2017_us_state_5m.shp")
US_shp_cropped <- crop(US_shp, extent(-124.848974, -66.885444, 24.396308, 49.384358))
# graph <- graph %>% mutate(state = as.factor(state))
US_shp_cropped@data <- left_join(US_shp_cropped@data, www, by = c("NAME" = "state"))
new_cells_hex <- calculate_grid(shape = US_shp_cropped, grid_type = "hexagonal", seed = 1)
result_hex <- assign_polygons(US_shp_cropped, new_cells_hex)
clean <- function(shape) {
shape@data$id = rownames(shape@data)
shape.points = fortify(shape, region="id")
shape.df = merge(shape.points, shape@data, by="id")
}
result_hex_df <- clean(result_hex)
font_add_google("Abel", "font")
showtext_auto()
ggplot(result_hex_df) +
geom_polygon(aes(x = long, y = lat, fill = mean, group = group), color = "black") +
geom_text(aes(V1, V2, label = STUSPS), size = 5, color = "white") +
scale_fill_gradient(low = "#EB5757", high = "#000000", name = "Percent Nones", labels = percent) +
theme_void() +
coord_equal() +
theme(legend.text=element_text(size=34)) +
labs(title = "Percent of Nones in Each State", caption = "Data: CCES 2018") +
theme(plot.title = element_text(family = "font", size = 24, vjust =2, face = "bold")) +
theme(plot.caption = element_text(family = "font", size = 12)) +
theme(legend.title = element_text(family = "font", size = 12, vjust =1.2, hjust = 0, face = "bold")) +
theme(legend.text=element_text(family = "font", face = "bold", size = 10)) +
# theme(legend.position="none") +
theme(plot.margin=unit(c(0,.5,0,.5),"cm")) +
ggsave("E://hexmap_nones.png")
library(socsci)
library(sf)
get_congress_map <- function(cong=113) {
tmp_file <- tempfile()
tmp_dir <- tempdir()
zp <- sprintf("http://cdmaps.polisci.ucla.edu/shp/districts%03i.zip",cong)
download.file(zp, tmp_file)
unzip(zipfile = tmp_file, exdir = tmp_dir)
fpath <- paste(tmp_dir, sprintf("districtShapes/districts%03i.shp",cong), sep = "/")
st_read(fpath)
}
cd114 <- get_congress_map(114)
small <- cd114 %>%
mutate(DISTRICT = as.character(DISTRICT)) %>%
mutate(STATENAME = as.character(STATENAME)) %>%
select(STATENAME, DISTRICT)
free <- tribble(
~STATENAME, ~ DISTRICT, ~free,
"California", 2, "yes",
"Maryland", 8, "yes",
"Tennessee", 9, "yes",
"Washington", 7,"yes",
"Georgia", 4, "yes",
"Michigan", 5, "yes",
"California", 19, "yes",
"California", 9, "yes",
"District Of Columbia", 98, "yes",
"Wisconsin", 2, "yes"
)
free <- free %>%
mutate(DISTRICT = as.character(DISTRICT))
map <- left_join(small, free)
map <- map %>%
filter(STATENAME != "Hawaii") %>%
filter(STATENAME != "Alaska")
register_google(key = "AIzaSyA4dSxmRotR0G_UKArfyp6yO2sDOBugDG4", account_type = "premium", day_limit = 100000)
myMap <- get_map(location = 'united states', zoom = 2)
ggplot() +
geom_sf(data = map, aes(fill = as.factor(free)), inherit.aes = FALSE, lwd =0) +
coord_sf(datum = NA) +
theme_gg("Abel") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "white")) +
labs(x= "", y = "", title = "Freethought Caucus Congressional Districts", caption = "@ryanburge") +
theme(legend.position = "none") +
theme(plot.title = element_text(family = "font")) +
ggsave("E://trying_cong_map.png")
ft <- read_csv("E://freethought.csv") %>% clean_names()
ft$state <- gsub("\\s*\\w*$", "", ft$district)
ft$state <- gsub("At-", "", ft$state)
ft$state <- gsub("\\'s", "", ft$state)
ft$state <- gsub("\\'", "", ft$state)
ft$state <- gsub(" ", "", ft$state)
library(haven)
library(labelled)
cces18 <- read_dta("D://cces18.dta")
www <- cces18 %>%
mutate(nones = car::recode(religpew, "9:11=1; else =0")) %>%
mutate(state = to_factor(inputstate)) %>%
group_by(state) %>%
mean_ci(nones, wt = commonweight) %>%
select(state, none_pct = mean) %>%
mutate(state = as.character(state))
ft1 <- left_join(ft, www)
ft1$med_income <- gsub("\\$", "", ft1$median_income)
ft1$med_income <- gsub("\\,", "", ft1$med_income)
ft1$med_income <- as.numeric(ft1$med_income)
reg1 <- glm(free ~ med_income + none_pct + black + latino + bachelors_degree_or_higher, data = ft1, family = "binomial")
gg1 <- effect_plot(reg1, pred = none_pct, interval = TRUE)
gg1 +
theme_gg("Abel") +
scale_y_continuous(labels = percent) +
scale_x_continuous(labels = percent) +
theme(plot.title = element_text(size = 14)) +
labs(x = "Percent of Nones in a State", y = "Likelihood of Election", title = "Predicting the Likelihood of Electing a Free Thought Caucus Member", caption = "@ryanburge") +
ggsave("E://effect_plot_ath.png")
mov <- tibble::tribble(
~name, ~margin,
"Huffman", 54,
"Raskin", 38,
"Cohen", 60,
"Jayapal", 67,
"Johnson", 57,
"Kildee", 24,
"Lofgren", 47,
"McNerney", 13,
"Norton", 83,
"Pocan", 100
)
mov %>%
ggplot(., aes(x = reorder(name, -margin), y = margin, fill = margin)) +
geom_col(color = "black") +
theme_gg("Abel") +
scale_y_continuous(labels = function(x) paste0(x, "%")) +
scale_fill_gradient(low = "firebrick3", high = "dodgerblue3") +
geom_text(aes(y = margin + 3, label = paste0(margin, '%')), position = position_dodge(width = .9), size = 4, family = "font") +
labs(y = "Margin of Victory", x = "", title = "Margin of Victory for Free Thought Caucus", caption = "@ryanburge\nData: Ballotpedia") +
ggsave("E://marginofvic.png", type = "cairo-png", width = 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment