Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
Created September 20, 2022 14:37
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 chrishanretty/1307b666f796395218c70bc3605c2804 to your computer and use it in GitHub Desktop.
Save chrishanretty/1307b666f796395218c70bc3605c2804 to your computer and use it in GitHub Desktop.
Proportion of CSES wave 5 respondents who can't place themselves on a left-right scale or don't know what it is
### Purpose of this code: plot the proportion of people in each CSES
### wave 5 study who can place themselves on a left-right scale.
### You will need to have downloaded the CSES 5 data somewhere
library(tidyverse)
library(ggtext)
library(RColorBrewer)
library(countrycode)
library(hrbrthemes)
cses5_locn <- "~/Downloads/cses5.rdata"
load(cses5_locn)
### Let's pick out only the variables we're interested in
cses5 <- cses5 %>%
select(country = E1006_NAM,
year = E1008,
lr_placement = E3020,
weight = E1010_3)
### Remove missing entries (99) or refused (97)
cses5 <- cses5 %>%
filter(lr_placement != 99) %>%
filter(lr_placement != 97)
### Create new variables
cses5 <- cses5 %>%
mutate(havent_heard_lr = lr_placement == 95,
dk_lr = lr_placement == 98)
### Get the proportions by country_year
cses5 <- cses5 %>%
group_by(country) %>%
filter(year == max(year)) %>%
summarize(havent_heard_lr = weighted.mean(havent_heard_lr,
weight),
dk_lr = weighted.mean(dk_lr,
weight),
.groups = "drop")
plot.df <- cses5 %>%
pivot_longer(cols = c(havent_heard_lr,
dk_lr)) %>%
group_by(country) %>%
mutate(sumvals = sum(value, na.rm = TRUE)) %>%
ungroup() %>%
mutate(country = fct_reorder(country,
sumvals))
### Plot this
p <- ggplot(plot.df, aes(x = country, y = value, fill = name)) +
geom_col() +
scale_x_discrete("") +
scale_y_continuous("% of respondents",
labels = scales::percent) +
scale_fill_discrete(type = brewer.pal(3, "Set1"), guide = "none") +
coord_flip() +
labs(title = "Proportion of respondents who <span style = 'color: #e41a1c'>haven't heard of left and right</span> or <br />who <span style = 'color: #377eb8'>can't place themselves on a 0-10 scale</span>",
subtitle = "Source: Comparative Study of Electoral Systems, wave 5. ") +
theme_ipsum_rc() +
theme(plot.title = element_markdown(),
plot.subtitle = element_markdown(),
plot.title.position = "plot")
ggsave(p, file = "cses_canplace.png", bg = "white", width = 9, height = 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment