library(tidyverse)
library(googlesheets4)
library(ggimage)
library(plotly)
# read in your data
gs4_deauth()
dt <- range_speedread(
"https://docs.google.com/spreadsheets/d/1npon5F_Gr40HQj8KVeHq_un7SOep4dzc6kjP0px7nLo/edit?gid=1750720002"
)
# reshape + summarise
platform <- c(
Q6_1 = "TikTok",
Q6_2 = "Twitter/X",
Q6_3 = "YouTube",
Q6_4 = "Television",
Q6_5 = "News"
)
dt_long <- dt %>%
pivot_longer(starts_with("Q6"), names_to = "Platform", values_to = "Score") %>%
mutate(
Platform = recode(Platform, !!!platform),
Score = as.numeric(Score)
) %>%
group_by(Platform) %>%
summarise(Average_Score = mean(Score, na.rm = TRUE), .groups = "drop") %>%
arrange(Average_Score)
#make the ggplot object
p <- ggplot(dt_long, aes(
x = Average_Score,
y = fct_reorder(Platform, Average_Score),
text = paste0(Platform, "\nScore: ", round(Average_Score,1))
)) +
geom_col(aes(fill = Average_Score), width = 0.7) +
scale_fill_viridis_c(option = "plasma", begin = 0.2, end = 0.8) +
labs(
title = "Average News Consumption Scores by Platform",
subtitle = "Based on Oct 2024 DU survey responses",
x = "Average Score (0–100)",
y = NULL,
caption = "Source: DU Survey October 2024"
) +
theme_minimal(base_size = 14) +
theme(
legend.position = "none",
plot.title = element_text(face = "bold", size = 16),
plot.subtitle = element_text(size = 12),
plot.caption = element_text(size = 10, color = "grey40")
)
# convert to plotly for hover & zoom
ggplotly(p, tooltip = "text") %>%
layout(
plot_bgcolor = 'rgba(0,0,0,0)',
paper_bgcolor = 'rgba(0,0,0,0)',
margin = list(l = 100, r = 40, t = 80, b = 40)
)Graph Challenge 9
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.