Created
November 4, 2025 02:03
-
-
Save thoughtfulbloke/dc1881ccb61c844bc0b2c1b9e33a029a to your computer and use it in GitHub Desktop.
using geom_col to regulate don't knows to below the main plot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(foreign) | |
| library(dplyr) | |
| library(tidyr) | |
| library(ggplot2) | |
| library(viridis) | |
| library(ggpattern) | |
| source("../theme.R") | |
| sav23 <- suppressWarnings(read.spss("2_NZES23Release_100227.sav", | |
| to.data.frame = TRUE, add.undeclared.levels = "sort")) | |
| meta23 <- data.frame(varnames = attributes(sav23)$names, | |
| descriptions = attributes(sav23)$variable.labels, | |
| stringsAsFactors = FALSE) | |
| # Validated Party Vote mvpartyvote | |
| # A11d: How much trust do you have in scientists? A11d | |
| # Validated Vote Comprehensive Vote Weight vwgt (everyone has a weight) | |
| # H27: Have you been vaccinated for COVID-19, and if so, how many times? H27 | |
| prosci <- sav23 |> | |
| mutate(trust = case_when(A11d == "9. Don’t know" ~ "Don't Know/\nDidn't Answer", | |
| is.na(A11d) ~ "Don't Know/\nDidn't Answer", | |
| TRUE ~ gsub(".*\\. ","",A11d))) |> | |
| summarise(.by=c(mvpartyvote, trust), | |
| weighted = sum(vwgt)) |> | |
| group_by(mvpartyvote) |> | |
| mutate(percentage= 100*weighted/sum(weighted), | |
| percent= ifelse(trust == "Don't Know/\nDidn't Answer", percentage * -1, percentage), | |
| party = ifelse(is.na(mvpartyvote),"unknown", gsub(".*\\. ","",mvpartyvote))) |> | |
| ungroup() | |
| # We have three columns we care about at this point, the x axis, the y axis, | |
| # and the fill (the fill includes the don't know category + Likert/Ordinal data) | |
| partorder <- prosci |> | |
| filter(trust %in% c("Trust a lot", "Trust somewhat")) |> | |
| summarise(.by=party, | |
| orderscore = sum(percent)) |> | |
| arrange(desc(orderscore)) | |
| vpal <- viridis_pal(alpha = 1, | |
| begin = 0, | |
| end = .9, | |
| direction = -1, | |
| option = "C")(4) | |
| vcol <- c("#EEEEEE",vpal) | |
| factrst <- c("Don't Know/\nDidn't Answer", "Trust a lot", "Trust somewhat", "Don’t trust very much", "Don’t trust at all") | |
| prosci |> | |
| mutate(party = factor(party, levels=partorder$party), | |
| trust = factor(trust, levels = factrst[c(1,5,4,3,2)])) |> | |
| ggplot(aes(x=party,y=percent)) + | |
| geom_col_pattern(aes(fill = trust, | |
| pattern_alpha = trust), | |
| colour="black",linewidth=0.05, | |
| pattern = 'plasma', | |
| pattern_fill = "grey0") + | |
| theme_david() + | |
| scale_fill_manual(name="Trust level", values=vcol) + | |
| scale_pattern_alpha_manual(name="Trust level", values = c(.5,0,0,0,0)) + | |
| scale_y_continuous(breaks=c(-15,0,25,50,75,100), | |
| labels=c(15,0,25,50,75,100)) + | |
| labs(title="NZES 2023: How much trust do you have in scientists? by party vote", | |
| subtitle="In order of trust, most trusting on left. Weighted percent.", | |
| y="Percent of voters", x="Party voted for", | |
| caption="Source: 2023 NZES")+ | |
| theme(axis.line.y = element_blank(), axis.line.x = element_blank())+ | |
| geom_hline(yintercept=0, linewidth=.3, colour="white") | |
| ggsave(filename="~/Desktop/trustsci.jpg",dpi=300, | |
| units="in", bg="white", height = 4.5, | |
| width=8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment