Graph Challenge 9

Author

Charvi Beniwal

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.

library(tidyverse)
library(googlesheets4)
library(readxl)
library(dplyr)
library(ggplot2)
library(plotly)
library(viridis)
url <- "https://docs.google.com/spreadsheets/d/1npon5F_Gr40HQj8KVeHq_un7SOep4dzc6kjP0px7nLo/edit?usp=sharing"
gs4_deauth()
dt <- range_speedread(url)
plot_data <- dt %>%
  dplyr::select(Q18_1, Q43_1, Q43_2, Q43_3) %>%
  rename(
    stress_level = Q18_1,
    need_to_be_perfect = Q43_1,
    fear_of_mistakes = Q43_2,
    doubts_about_actions = Q43_3
  ) %>%
  filter(!is.na(stress_level), 
         !is.na(need_to_be_perfect), 
         !is.na(fear_of_mistakes), 
         !is.na(doubts_about_actions)) %>%
  mutate(stress_category = case_when(
    stress_level <= 20 ~ "Low (0-20)",
    stress_level <= 40 ~ "Mild (21-40)",
    stress_level <= 60 ~ "Moderate (41-60)",
    stress_level <= 80 ~ "High (61-80)",
    TRUE ~ "Very High (81-100)"
  ),
  need_to_be_perfect_label = factor(need_to_be_perfect, levels = 1:5, 
                                   labels = c("Strongly Agree", "Agree", "Neither", "Disagree", "Strongly Disagree")),
  fear_of_mistakes_label = factor(fear_of_mistakes, levels = 1:5, 
                                 labels = c("Strongly Agree", "Agree", "Neither", "Disagree", "Strongly Disagree")),
  doubts_about_actions_label = factor(doubts_about_actions, levels = 1:5, 
                                     labels = c("Strongly Agree", "Agree", "Neither", "Disagree", "Strongly Disagree")),
  stress_category = factor(stress_category,levels = c("Low (0-20)", "Mild (21-40)", "Moderate (41-60)", "High (61-80)", "Very High (81-100)"))
  )
summary_data <- plot_data %>%
  group_by(stress_category, need_to_be_perfect_label) %>%
  summarise(count = n(), .groups = "drop") %>%
  group_by(stress_category) %>%
  mutate(percentage = count / sum(count) * 100,
         total = sum(count))
p <- ggplot(summary_data, aes(x = stress_category, y = percentage, fill = need_to_be_perfect_label)) +
  geom_bar(stat = "identity", position = "stack") +
  scale_fill_viridis_d(option = "magma", direction = -1) +
  labs(
    title = "Relationship Between Stress Levels and Perfectionism",
    subtitle = "From Denison University April 2025 Survey",
    x = "Stress Level Category",
    y = "Percentage of Responses",
    fill = "Response to:'I have a strong\nneed to be perfect'"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", size = 14),
    plot.subtitle = element_text(size = 11, color = "gray30"),
    axis.title = element_text(size = 12),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = "right",
    legend.title = element_text(size = 11)
  )
ggplotly(p, tooltip = c("x", "y", "fill")) %>%
  layout(
    hovermode = "closest",
    hoverlabel = list(bgcolor = "lightblue", font = list(size = 12)),
    annotations = list(
      x = 1, y = -0.15, 
      text = ".",
      showarrow = FALSE,
      xref = 'paper', yref = 'paper',
      xanchor = 'right', yanchor = 'auto',
      font = list(size = 10)
    )
  )

The visualization demonstrates a clear correlation between stress levels and perfectionist tendencies among Denison students. As stress levels increase from low to very high, there’s a consistent increase in students who agree they have “a strong need to be perfect.” Students with the highest stress levels (81-100) show the strongest perfectionist tendencies, while those with lower stress exhibit more varied responses.

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.