Plotly

# install.packages("plotly")
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(googlesheets4) # install.packages("googlesheets4")
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
library(socsci)
Loading required package: rlang

Attaching package: 'rlang'

The following objects are masked from 'package:purrr':

    %@%, flatten, flatten_chr, flatten_dbl, flatten_int, flatten_lgl,
    flatten_raw, invoke, splice

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
url <- "https://docs.google.com/spreadsheets/d/1npon5F_Gr40HQj8KVeHq_un7SOep4dzc6kjP0px7nLo/edit?usp=sharing"
gs4_deauth()
dt <- range_speedread(url)
✔ Reading from "April_2025".
ℹ Export URL:
  <https://docs.google.com/spreadsheets/d/1npon5F_Gr40HQj8KVeHq_un7SOep4dzc6kjP0px7nLo/export?format=csv>
Rows: 273 Columns: 274
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr   (9): StartDate, EndDate, RecordedDate, ResponseId, Q24_6_TEXT, Q32_13_...
dbl (265): Status, Progress, Duration (in seconds), Finished, Q1, Q2, Q3, Q4...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
dt |>
  mutate(
    aid = ifelse(
      is.na(Q34), Q35, Q34
    ),
    aid = frcode(
      aid == 1 ~ "Too much",
      aid == 2 ~ "Just right",
      aid == 3 ~ "Not enough"
    ),
    treat = frcode(
      !is.na(Q34) ~ "No",
      !is.na(Q35) ~ "Yes"
    )
  ) -> dt
smdt <- dt |>
  group_by(treat) |>
  ct(aid, show_na = F)
ggplot(smdt) +
  aes(pct, aid, fill = treat) +
  geom_col(
    position = "dodge"
  ) +
  labs(
    title = 
      str_wrap(
        "Does the US spend too much, just the right amount, or not enough on foreign aid?",
        width = 50
      ),
    x = "% of respondents",
    y = NULL,
    fill = str_wrap(
      "Told the US spends 1% of its budget on aid",
      width = 17
    )
  ) +
  scale_x_continuous(
    labels = scales::percent
  ) -> p
p

ggplotly(p)