if(!require(here)){
install.packages("here")
}library(here)
Tutorial-04
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 thetidyverse
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 calleddata
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
- Read your data set into R and follow the Reading Data Checklist from the lecture.
We recommend installing the packagehere
to help keep things organised when referencing files.
library(tidyverse)
<- read_csv(here("data", "boston_celtics.csv")) boston_celtics
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)
<- read_csv(here("data", "boston_celtics.csv"))
boston_celtics
# Look at your data
View(boston_celtics)
# Look at a summary
summary(boston_celtics)
- Create a scatter plot that shows the
game_date
andteam_scores
. Change the colour of the points usingteam_winner
to show if the team won the game. Make the colour of the pointsforestgreen
. Focus on the geometry and aesthetic layer for now.
- 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.
- 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.
- Add appropriate labels to your plot and edit the legend position.
- 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) andthree_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
.