2024-10-09
Infographic is due Friday night!
You should be working on your presentations for Jamie…
Walked through basics of text data with bag-of-words representation
Created word cloud visualizations of counts and bar charts of TF-IDF values
TODAY:
Discuss the role of animations
Walk through interactive visualizations
Introduction to Shiny for creating apps
gganimate
to add animationsWe could incrementally reveal the results at each stage to emphasize the story of progression
library(gganimate)
f1_data_ex |>
ggplot(aes(x = round, y = points, group = name, color = name)) +
geom_line(size = 2) +
scale_x_continuous(breaks = seq(1, 17, 1)) +
labs(title = "The race for third place in the 2020 F1 season",
y = "Accumulated points", x = NULL) +
theme_bw() +
transition_reveal(round)
gganimate
to add animationstxhousing |>
group_by(city, year) |>
summarize(median = mean(median, na.rm = TRUE),listings = mean(listings, na.rm = TRUE)) |>
ggplot(aes(x = median, y = listings, color = (city == "Houston"),
size = (city == "Houston"))) +
geom_point(alpha = 0.5, show.legend = FALSE) +
scale_color_manual(values = c("black", "darkred")) +
scale_size_manual(values = c(2, 4)) +
scale_x_continuous(labels = scales::dollar, name = "Median Price") +
scale_y_continuous(labels = scales::label_number(scale_cut = scales::cut_short_scale())) +
theme_bw() +
labs(x = "Median Price", y = "Avg. of Monthly Listings",
subtitle = "Houston in red")
txhousing |>
group_by(city, year) |>
summarize(median = mean(median, na.rm = TRUE), listings = mean(listings, na.rm = TRUE)) |>
ggplot(aes(x = median, y = listings, color = (city == "Houston"),
size = (city == "Houston"))) +
geom_point(alpha = 0.5, show.legend = FALSE) +
scale_color_manual(values = c("black", "darkred")) +
scale_size_manual(values = c(2, 4)) +
scale_x_continuous(labels = scales::dollar, name = "Median Price") +
scale_y_continuous(labels = scales::label_number(scale_cut = scales::cut_short_scale())) +
theme_bw() +
labs(x = "Median Price", y = "Avg. of Monthly Listings",
subtitle = "Houston in red", title = "Year: {frame_time}") +
transition_time(year)
Some key points to think about before adding animation to a visualization:
R
and JavaScriptJavaScript enables web developers to create client-side web applications
Computations are happening in the client’s browser, instead of the host’s web servers
D3
(or D3.js
) is the most popular JavaScript library for client-side dynamic data visualizations
D3
== ‘data-driven documents’RStudio developers created the htmlwidgets
enabling R
users to use D3
without needing to learn JavaScript!
Renders data visualizations in HTML using D3
wrappers
rchess
!addTiles()
: builds layer with static map (default from OpenStreetMap)
addMarkers()
: add marker at point location, e.g., CMU
Use tidygeocoder
for spatial queries
Dashboards are popular way to make data and visualizations available to clients, managers, stakeholders, etc. to help with decision making
Typically include a mix of graphics and text, depending on the context
Can easily make HTML dashboards using the flexdashboard
package
Provide interactive capabilities within an HTML file (i.e., you can email this!)
These are NOT fully interactive like Shiny apps
You can use Shiny within flexdashboard
but it requires a Shiny server
Another option is shinydashboard
More recently, Quarto dashboards are now possible
Outside of R
, Tableau is a popular tool (can use free Tableau Public version)
Shiny
Shiny is used to build interactive web applications in R
You do NOT need to be a web developer to create Shiny apps, you just need to learn some additional syntax to augment your R
code
Every Shiny app consists of two scripts (can be saved into one file app.R
)
ui.R
: controls user interface, sets up the display, widgets for user input
server.R
: code to generate / display the results! Communicates with ui.R
with reactive objects: processes user input
to return output
R
code: load packages, data wrangling, create plotsCan be run locally or deployed on a Shiny app server for public viewing
Discussed the role of animation in visualizations
Overview of different interactive elements and basics of Shiny
I never discussed making fancy tables, but check out the gt
package
Infographic is due Friday night!
Hope you enjoyed the class, please complete the FCEs!
More reading: gganimate
package, using Shiny within Quarto, Mastering Shiny book