Tutorial-04

Author

Kate Saunders

Visualisation in R

Learning Objectives

  • Practice how to create plots in R.

  • This will involve understanding the grammar of graphics and what each of the different layers are.

  • You will learn to create a range of different plots.

Preparation

  • Ensure you installed R and RStudio.

  • Ensure you have installed the ggplot2 package. You can install this package directly, but this package is also part of the tidyverse package.

  • Before you get started set yourself up an R project. This will help you to direct R to where your data is installed.

  • If you need any help doing the above, refer to the Lecture 1 and Tutorial 1 material.

  • Download the dataset from Moodle, boston_celtics.csv, and place it in a folder called data within your R Project.

Task

Today you will be creating visualisations in ggplot2 to analyse sporting statistics from the Boston Celtics NBA basketball team.

Exercise 1

  1. Read your data set into R and follow the Reading Data Checklist from the lecture.

    We recommend installing the package here to help keep things organised when referencing files.
if(!require(here)){
  install.packages("here")
}
library(here)
library(tidyverse)
boston_celtics <- read_csv(here("data", "boston_celtics.csv"))

If you have an trouble run through the check list:

# Check your working directory
getwd()  

# Check your data file is where you think it is
file.exists(here("data","boston_celtics.csv"))

# Read in your data 
library(tidyverse)
boston_celtics <- read_csv(here("data", "boston_celtics.csv"))

# Look at your data 
View(boston_celtics)

# Look at a summary
summary(boston_celtics)
  1. Create a scatter plot that shows the game_date and team_scores. Change the colour of the points using team_winner to show if the team won the game. Make the colour of the points forestgreen. Focus on the geometry and aesthetic layer for now.
  1. Colour the points according to if the team won or lost. Use the hex colour codes from the Boston Celtics team to set the colour scheme.
  1. As there are a lot of points, change the size and the transparency to reduce overplotting.

5.Look at the theme options and try using a few different ones, before deciding on the theme for your plot.

  1. Add appropriate labels to your plot and edit the legend position.
  1. Move the legend on the plot to increase the data-density. (Note the solutions show a more advanced way to move the legend that the lecture notes.)

Exercise 2

Now we’ve mastered the basic layers, take some time to creating some other visualisations that are interesting to you!

  • You may like to produce plots looking at the distribution of some of the key variables. For example, assists, rebounds, steals etc.
  • Maybe you are interested in how the shot percentage varies between field_goal_pct (2 pts) and three_point_field_goal_pct (3 pts).
  • Perhaps you want to create a bar chart (geom_bar) to see how many times they’ve played opposing teams this season.

Finishing Up

By the end of this tutorial you should feel comfortable reading in a data set into R and creating a visualisation but adding layers in ggplot2.

Material developed by Dr. Kate Saunders.