library(socsci)

cces16 <- read_csv("https://raw.githubusercontent.com/ryanburge/cces/master/CCES%20for%20Methods/small_cces.csv")

Let’s Compare Hispanics with a College Degree to Whites Without a College Degree on Vote Choice in 2016

aaa1 <- cces16 %>% 
  filter(race ==3) %>% 
  filter(educ == 5 | educ == 6) %>% 
  filter(vote16 <=3) %>% 
  ct(vote16) %>% 
  mutate(group = "Hispanics with a College Degree")

aaa2 <- cces16 %>% 
  filter(race ==1) %>% 
  filter(educ != 5 | educ != 6) %>% 
  filter(vote16 <=4) %>% 
  ct(vote16) %>% 
  mutate(group = "Whites without a College Degree")

Let’s Bind our Two Datasets Together

graph <- bind_df("aaa")


graph 
## # A tibble: 7 x 4
##   vote16     n   pct group                          
##    <int> <int> <dbl> <chr>                          
## 1      1   333 0.298 Hispanics with a College Degree
## 2      2   742 0.663 Hispanics with a College Degree
## 3      3    44 0.039 Hispanics with a College Degree
## 4      1 16536 0.491 Whites without a College Degree
## 5      2 14940 0.444 Whites without a College Degree
## 6      3  1522 0.045 Whites without a College Degree
## 7      4   655 0.019 Whites without a College Degree

And Recode Our Vote Variable

graph <- graph %>% 
  mutate(vote16 = frcode(vote16 == 1 ~ "Trump", 
                         vote16 == 2 ~ "Clinton", 
                         vote16 == 3 ~ "Johnson", 
                         vote16 == 4 ~ "Stein", 
                         TRUE ~ "REMOVE"))

And Graph

graph %>% 
  ggplot(., aes(x=vote16, y = pct, fill = group)) +
  geom_col(color = "black", position = "dodge") +
  scale_fill_manual(values = c("dodgerblue", "azure3")) +
  theme_minimal() +
  scale_y_continuous(labels = scales::percent) +
  theme(legend.position = "bottom") + 
  theme(legend.title =element_blank())

Or You Could Facet It

graph %>% 
  ggplot(., aes(x=vote16, y = pct, fill = vote16)) +
  geom_col(color = "black") +
  facet_grid(~ group) +
  scale_fill_manual(values = c("firebrick3", "dodgerblue", "goldenrod", "forestgreen")) +
  theme_minimal() +
  scale_y_continuous(labels = scales::percent) +
  theme(legend.position = "none") + 
  theme(legend.title =element_blank())