Skip to content

Instantly share code, notes, and snippets.

@svmiller
Last active June 1, 2017 16:21
Show Gist options
  • Save svmiller/4ffa7d6a16ae4f28ae17b102184a86a4 to your computer and use it in GitHub Desktop.
Save svmiller/4ffa7d6a16ae4f28ae17b102184a86a4 to your computer and use it in GitHub Desktop.
library(RCurl)
library(tidyverse)
library(maps)
# library(fiftystater)
theme_steve <- function() {
theme_bw() +
theme(panel.border = element_blank(),
plot.caption=element_text(hjust=1, size=9,
margin=margin(t=10),
face="italic"),
plot.title=element_text(hjust=0, size=18,
margin=margin(b=10),
face="bold"),
axis.title.y=element_text(size=12,hjust=1,
face="italic"),
axis.title.x=element_text(hjust=1, size=12, face="italic"))
}
data <- getURL("https://raw.githubusercontent.com/svmiller/2016-cces-trump-vote/master/2016-cces-trump.csv")
Data <- read.csv(text = data) %>% tbl_df()
Data %>%
filter(lcograc >= .5) %>%
group_by(state) %>%
summarize(lcograc=n()) -> cogracists
Data %>%
group_by(state) %>%
summarize(population = n()) %>%
left_join(cogracists, .) %>%
mutate(`Percentage` = round((lcograc/population)*100, 2),
region = tolower(state)) -> cogracists
cogracists <- left_join(states, cogracists, by="region") %>%
arrange(order)
ggplot(cogracists, aes(x=long,y=lat,group=group))+
geom_polygon(aes(fill=Percentage))+
geom_path()+
scale_fill_gradientn(colours=rev(heat.colors(10)),na.value="grey90")+
coord_map() + theme_steve() + xlab("") + ylab("") +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
ggtitle("Distribution of Cognitive Racists in the 2016 CCES Data") +
labs(subtitle="'Cognitive racists' defined as those scoring above a standard deviation in the cognitive racism scale. Values communicate number of cognitive racists over number of state respondents in CCES data.",
caption = "Questions comprising cognitive racism scale include whether whites have certain advantages because of skin color and whether racial problems are rare isolated situations.
Source: http://svmiller.com/blog/2017/04/age-income-racism-partisanship-trump-vote-2016/")
Data %>%
filter(lcograc >= .5 & dem == 1) %>%
group_by(state) %>%
summarize(lcograc=n()) -> cogracistsdem
Data %>%
filter(dem == 1) %>%
group_by(state) %>%
summarize(population = n()) %>%
left_join(cogracistsdem, .) %>%
mutate(`Percentage` = round((lcograc/population)*100, 2),
region = tolower(state)) -> cogracistsdem
cogracistsdem <- left_join(states, cogracistsdem, by="region") %>%
arrange(order)
ggplot(cogracistsdem, aes(x=long,y=lat,group=group))+
geom_polygon(aes(fill=Percentage))+
geom_path()+
scale_fill_gradientn(colours=rev(heat.colors(10)),na.value="grey90")+
coord_map() + theme_steve() + xlab("") + ylab("") +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
ggtitle("Distribution of Cognitive Racists among Democrats in the 2016 CCES Data") +
labs(subtitle="Values communicate number of cognitive racist Democrats over number of state-level Democrat respondents in CCES data.",
caption = "Questions comprising cognitive racism scale include whether whites have certain advantages because of skin color and whether racial problems are rare isolated situations.
Source: http://svmiller.com/blog/2017/04/age-income-racism-partisanship-trump-vote-2016/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment