if(!require(here)){ install.packages("here") }
library(here)
if(!require(hoopR)){ install.packages("hoopR") }
library(hoopR)
library(tidyverse)Tutorial-04
Visualisation in R
Replace each ??? with your own code. Then change eval: false to eval: true in that chunk so it runs when you render.
Exercise 1
Question 1: Get the data
Run these two chunks.
Then read in your data. The data is only downloaded if you don’t already have the file saved.
data_file <- here("data", "boston_celtics.csv")
if (!file.exists(data_file)) {
# Get the NBA team data
progressr::with_progress({
nba_team_box <- hoopR::load_nba_team_box(2021:hoopR::most_recent_nba_season())
})
# Filter to the Boston Celtics
boston_celtics <- nba_team_box |>
filter(team_abbreviation == "BOS")
# Save the data out
dir.create(here("data"), showWarnings = FALSE)
write_csv(boston_celtics, file = data_file)
}
# Read in your data
boston_celtics <- read_csv(data_file)Question 2: Scatter plot
Plot game_date and team_score, with forestgreen points.
Question 3: Colour by result
Colour the points by whether the team won or lost. Notice that the colour command now moves inside the bracket specify aesthetic mappings - Make sure you understand why this is.
Question 4: Team colours
Use the hex colour codes from the Boston Celtics team to set the colour scheme.
Question 5: Size and transparency
Change the size and transparency of the points.
Question 6: Theme
Try a few themes, then choose one.
Question 7: Labels
Add appropriate labels.
Question 8: Legend
Move the legend to increase the data-density.
Exercise 2
Now we’ve mastered the basic layers, experiment with using AI to create some other visualisations!
Here are some example prompts for you to try.
Distributions
I am a beginner learning how to create distribution plots in R using ggplot2.
I have a data frame in R called boston_celtics, with one row for each game played by the Boston Celtics.
The column assists is numeric and team_winner is TRUE or FALSE.
Please show me examples of a histogram, a density plot and a boxplot.
Explain each line of code and when I would use each plot.
What I checked or changed:
Relationships
I am looking at different ways to plot the relationship between two numeric variables in R using ggplot2.
I have a data frame in R called boston_celtics, with one row for each game played by the Boston Celtics.
The columns assists, team_score, field_goal_pct and three_point_field_goal_pct are numeric, and team_winner is TRUE or FALSE.
Please show me a few different plot options, including one that maps a third variable to colour.
Explain the strengths and weaknesses of each.
What I checked or changed:
Beeswarm plots
I am a beginner and have not used the ggbeeswarm package in R before.
I have a data frame in R called boston_celtics, with one row for each game played by the Boston Celtics.
The column assists is numeric and team_winner is TRUE or FALSE.
Please explain what a beeswarm plot is, how it differs from a boxplot, and show me an example comparing assists in wins and losses.
What I checked or changed:
In Your Own Time
Keep experimenting by writing your own prompts to create new plots. Try different variables, geometries and ways of using colour, and change how much you tell the AI about yourself and your data.
Decide what question you want the plot to answer, then describe the data, variables and plot type in your prompt.
Run the code and check it does what you asked.
Review the code line by line and aim to understand the idea of what it does, particularly how it uses the grammar of graphics: the data, aesthetic mappings, geometries and other layers.
Ask the AI about anything you don’t understand, and check its answer against the
ggplot2documentation or lecture notes.
My prompt:
What I checked or changed: