Graph Challenge 9

Author

Bach Nguyen

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 setup

library(tidyverse)
library(plotly)
library(googlesheets4)
library(socsci)   # for frcode()

gs4_deauth()

my_theme <- function(){
  theme_bw(
    base_family = "sans"
  ) +
    theme(
      plot.title = element_text(face = "bold", size = 15),
      plot.subtitle = element_text(size = 12),
      plot.title.position = "plot",
      plot.caption = ggtext::element_markdown(),
      axis.title.x = element_text(
        hjust = .5,
        size = 12
      ),
      axis.title.y = element_text(
        hjust = .5,
        size = 12
      ),
      axis.ticks.length.x = unit(0, "cm"),
      axis.ticks.length.y = unit(0, "cm"),
      
    ) 
}

Data Filtering

dt <- range_speedread("https://docs.google.com/spreadsheets/d/1npon5F_Gr40HQj8KVeHq_un7SOep4dzc6kjP0px7nLo/edit?usp=sharing")

dt <- dt %>%
  mutate(
    reading_hours = as.numeric(Q22_1),
    grad_year = as.numeric(Q56)
  )

dt <- dt %>%
  mutate(
    class_year = frcode(
      grad_year == 1 ~ "Senior",
      grad_year == 2 ~ "Junior",
      grad_year == 3 ~ "Sophomore",
      grad_year == 4 ~ "Freshman",
      FALSE ~ NA_character_
    )
  )

dt_clean <- dt %>%
  filter(!is.na(reading_hours), !is.na(class_year))

summary_dt <- dt_clean %>%
  group_by(class_year) %>%
  summarize(avg_reading_hours = mean(reading_hours, na.rm = TRUE))

Plot

p <- ggplot(summary_dt, aes(x = class_year, y = avg_reading_hours, group = 1)) +
  geom_line() +
  geom_point() +
  scale_x_discrete(limits = c("Freshman", "Sophomore", "Junior", "Senior")) + 
  labs(
    title = "Average Number of Reading Assignments Fully Completed by Class Year",
    x = "Class Year",
    y = "Average Number of Reading Assignments"
  ) +
  my_theme()

p_interactive <- ggplotly(p)

p_interactive