library(building)
## Loading required package: tidyverse
## -- Attaching packages ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ tidyverse 1.2.1 --
## v ggplot2 3.0.0     v purrr   0.2.5
## v tibble  1.4.2     v dplyr   0.7.6
## v tidyr   0.8.1     v stringr 1.3.1
## v readr   1.1.1     v forcats 0.3.0
## -- Conflicts --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## Loading required package: rlang
## 
## Attaching package: 'rlang'
## The following objects are masked from 'package:purrr':
## 
##     %@%, %||%, as_function, flatten, flatten_chr, flatten_dbl,
##     flatten_int, flatten_lgl, invoke, list_along, modify, prepend,
##     rep_along, splice
## Loading required package: showtext
## Loading required package: sysfonts
## Loading required package: showtextdb
## Loading required package: scales
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
## Loading required package: broom
## Loading required package: glue
## 
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
## 
##     collapse
cces <- read_csv("https://raw.githubusercontent.com/ryanburge/cces/master/CCES%20for%20Methods/small_cces.csv")
## Warning: Missing column names filled in: 'X1' [1]
## Warning: Duplicated column names deduplicated: 'X1' => 'X1_1' [2]
## Parsed with column specification:
## cols(
##   .default = col_integer()
## )
## See spec(...) for full column specifications.

Using Regular case_when

graph <- cces %>% 
  mutate(pid_new = case_when(pid7 == 1 ~ "Strong Democrat", 
                                 pid7 == 2 ~ "Not Strong Democrat", 
                                 pid7 == 3 ~ "Lean Democrat", 
                                 pid7 == 4 ~ "Independent", 
                                 pid7 == 5 ~ "Lean Republican", 
                                 pid7 == 6 ~ "Not Strong Republican", 
                                 pid7 == 7 ~ "Strong Republican", 
                                 TRUE ~ "REMOVE")) %>% 
  ct(pid_new)


graph %>% 
  filter(pid_new != "REMOVE") %>% 
  ggplot(., aes(x = pid_new, y = pct)) +
  geom_col()

Using fct_case_when

fct_case_when <- function(...) {
  args <- as.list(match.call())
  levels <- sapply(args[-1], function(f) f[[3]])  # extract RHS of formula
  levels <- levels[!is.na(levels)]
  factor(dplyr::case_when(...), levels=levels)
}



graph <- cces %>% 
  mutate(pid_new = fct_case_when(pid7 == 1 ~ "Strong Democrat", 
                                 pid7 == 2 ~ "Not Strong Democrat", 
                                 pid7 == 3 ~ "Lean Democrat", 
                                 pid7 == 4 ~ "Independent", 
                                 pid7 == 5 ~ "Lean Republican", 
                                 pid7 == 6 ~ "Not Strong Republican", 
                                 pid7 == 7 ~ "Strong Republican", 
                                 TRUE ~ "REMOVE")) %>% 
  ct(pid_new)


graph %>% 
  filter(pid_new != "REMOVE") %>% 
  ggplot(., aes(x = pid_new, y = pct)) +
  geom_col()