Graph Challenge 9

Author

Tran Ong

Published

April 28, 2025

The {plotly} package lets you turn your ggplot figures into interactive ones. You can learn more about it here: https://plotly.com/ggplot2/

For this graph challenge, I want you to create an interactive plot with the April 2025 DU survey data using ggplot and plotly. The type of plot is up to you. So is the variable or variables you’d like to show.

When you’re done, rather than hitting the “render” button, hit the “publish” button to publish your submission to RPubs. Then, submit the link to your publication on Canvas.

library(tidyverse)
library(googlesheets4)
gs4_deauth()
range_speedread(
  "https://docs.google.com/spreadsheets/d/1npon5F_Gr40HQj8KVeHq_un7SOep4dzc6kjP0px7nLo/edit?pli=1&gid=1750720002#gid=1750720002"
) -> dt
# Filter only Q3 in 1, 2, 3 and select only needed columns
dt_filtered <- dt %>%
  filter(Q3 %in% c(1, 2, 3)) %>%
  select(
    Q3,
    starts_with("Q5_"),
    starts_with("Q6_"),
    starts_with("Q14_"),
    starts_with("Q32_")
  )
dt_filtered
# A tibble: 200 × 40
      Q3  Q5_1  Q5_2  Q5_3  Q6_1  Q6_2  Q6_3  Q6_4  Q6_5 Q14_1 Q14_2 Q14_3 Q14_4
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1     3     4     3     2    95     1    NA    NA    NA     3     3     3     3
 2     2     2     2     2    15     0     0    40    45     2     2     3     3
 3     3     2     2     1     5    NA    NA    NA    89     2     2     2     2
 4     2     4     2     3    73     0     0    16    66     2     2     2     3
 5     1     2     2     2     0     0     0    40    90     1     2     2     2
 6     3     1     1     1    75    50    NA    NA    90     3     4     3     5
 7     1     2     2     2     0    10     5    60    35     3     3     3     3
 8     1     4     2     2    NA    NA    53    15    51     3     3     3     5
 9     3     3     1     1    NA    35    77    91   100     1     1     1     1
10     2     2     2     2     0     0    48    NA    50     2     2     2     2
# ℹ 190 more rows
# ℹ 27 more variables: Q32_1 <dbl>, Q32_2 <dbl>, Q32_3 <dbl>, Q32_4 <dbl>,
#   Q32_5 <dbl>, Q32_6 <dbl>, Q32_7 <dbl>, Q32_8 <dbl>, Q32_9 <dbl>,
#   Q32_10 <dbl>, Q32_11 <dbl>, Q32_12 <dbl>, Q32_13 <dbl>, Q32_13_TEXT <chr>,
#   Q32_DO_1 <dbl>, Q32_DO_2 <dbl>, Q32_DO_3 <dbl>, Q32_DO_4 <dbl>,
#   Q32_DO_5 <dbl>, Q32_DO_6 <dbl>, Q32_DO_7 <dbl>, Q32_DO_8 <dbl>,
#   Q32_DO_9 <dbl>, Q32_DO_10 <dbl>, Q32_DO_11 <dbl>, Q32_DO_12 <dbl>, …
library(plotly)
library(dplyr)

dt_filtered <- dt_filtered %>%
  mutate(
    Q3_cat = case_when(
      Q3 == 1 ~ "Democrat",
      Q3 == 2 ~ "Lean Democrat",
      Q3 == 3 ~ "Independent but Lean Democrat"
    ),
    Q5_1_cat = factor(Q5_1,
                      levels = c(1, 2, 3, 4, 5),
                      labels = c("Strongly agree", "Agree", "Neutral", "Disagree", "Strongly disagree"),
                      ordered = TRUE)
  )

# Create the interactive stacked bar chart
fig <- dt_filtered %>%
  plot_ly(x = ~Q3_cat, color = ~Q5_1_cat, type = "histogram") %>%
  layout(
    barmode = "stack",
    title = "Positive Feelings Toward People with Different Viewpoints on Campus",
    xaxis = list(title = "Political Affiliation"),
    yaxis = list(title = "Number of Respondents"),
    legend = list(title = list(text = "Level of Agreement"))
  )

# Show the plot
fig