Skip to content

Instantly share code, notes, and snippets.

@JoFrhwld
Created February 21, 2018 17:52
Show Gist options
  • Save JoFrhwld/ced108c6da1c5ceaa1d32120bd4b6be8 to your computer and use it in GitHub Desktop.
Save JoFrhwld/ced108c6da1c5ceaa1d32120bd4b6be8 to your computer and use it in GitHub Desktop.
---
title: "R Notebook"
output: html_notebook
---
Source:
https://edition.cnn.com/election/2016/results/exit-polls
```{r}
"Eth Age Share Clinton Trump Other
whites 18-29 12 43 47 10
latinos 30-44 4 65 28 7
latinos 45-64 4 64 32 4
latinos 65 and older 1 73 25 2
whites 30-44 16 37 54 9
whites 45-64 30 34 62 4
whites 65 and older 13 39 58 3
blacks 18-29 3 85 9 6
blacks 30-44 4 89 7 4
blacks 45-64 5 90 9 1
blacks 65 and older 1 91 9 n/a
latinos 18-29 3 68 26 6" -> exitpoll_str
con <- textConnection(exitpoll_str)
exitpoll <- read.delim(con)
close(con)
```
```{r}
exitpoll
```
```{r}
exitpoll%>%
ggplot(aes(Age, Clinton/Trump))+
geom_hline(yintercept=1)+
geom_point(aes(color = Eth))+
geom_line(aes(group = Eth, color = Eth))+
scale_y_continuous(trans = "log2", breaks = c(0.5, 1, 2, 4, 8, 16))+
scale_color_brewer(palette = "Dark2")+
theme_minimal()
```
```{r}
exitpoll%>%
mutate(Age2 = Age)%>%
separate(Age2, into = c("low", "high"), sep = "-")%>%
mutate(low = gsub("and older", "", low),
low_2003 = as.numeric(low) - 13,
high_2003 = as.numeric(high) - 13,
Age_2003 = paste(low_2003, high_2003, sep = "-"),
Age_2003 = gsub("-NA", " and older", Age_2003),
Age_2003 = factor(Age_2003),
Age_2003 = reorder(Age_2003, low_2003, FUN = mean))%>%
ggplot(aes(Age_2003, Clinton/Trump))+
geom_hline(yintercept=1)+
geom_point(aes(color = Eth))+
geom_line(aes(group = Eth, color = Eth))+
scale_y_continuous(trans = "log2", breaks = c(0.5, 1, 2, 4, 8, 16))+
scale_color_brewer(palette = "Dark2")+
theme_minimal()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment