Import your data

data <- read_csv('https://storage.googleapis.com/kagglesdsdata/datasets/8895632/14065905/premier_league_table_13.csv?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=gcp-kaggle-com%40kaggle-161607.iam.gserviceaccount.com%2F20251209%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20251209T053343Z&X-Goog-Expires=259200&X-Goog-SignedHeaders=host&X-Goog-Signature=1552228db435a2c48eb9b1c1da7e40d23078189f8c18012bd5f853f29725694f7c1453b4cea21720ee3246831af2daac9f4aa01e69ea29d0f8992ae3ab9a9814d67636e4ec8f8fcf9c5b95ab74dbb0c0db6bfc76921261eddb2245035cf41548e334d59718517e773a3b6d9b63ecbe066c6233e99b5e05adbd22539b45dfa5e2cf55b6b63dda91041ba7b624fd558a515a45bcb239c36fd26ede08a0d1c325e8c380ce74442d5725d77832192fc25c17ea6326197a1f926ff7191981ade0bc651d75e0f57dee13c5e8a29ae23dc7238cd4f58065f8f037b54035dc6448ed114ccd8f26f94247f2794172df50c581ee02c29206c16036900da4a201530dc8aca6')
## Rows: 20 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Team
## dbl (8): Played, Won, Drawn, Lost, Goals For, Goals Against, Goal Difference...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Chapter 15

Create a factor

names(data)
## [1] "Team"            "Played"          "Won"             "Drawn"          
## [5] "Lost"            "Goals For"       "Goals Against"   "Goal Difference"
## [9] "Points"
data$Team <- factor(data$Team)
data$Team
##  [1] Arsenal                 Manchester City         Chelsea                
##  [4] Aston Villa             Brighton & Hove Albion  Sunderland             
##  [7] Manchester United       Liverpool               Crystal Palace         
## [10] Brentford               AFC Bournemouth         Tottenham Hotspur      
## [13] Newcastle United        Everton                 Fulham                 
## [16] Nottingham Forest       West Ham United         Leeds United           
## [19] Burnley                 Wolverhampton Wanderers
## 20 Levels: AFC Bournemouth Arsenal Aston Villa ... Wolverhampton Wanderers
levels(data$Team)
##  [1] "AFC Bournemouth"         "Arsenal"                
##  [3] "Aston Villa"             "Brentford"              
##  [5] "Brighton & Hove Albion"  "Burnley"                
##  [7] "Chelsea"                 "Crystal Palace"         
##  [9] "Everton"                 "Fulham"                 
## [11] "Leeds United"            "Liverpool"              
## [13] "Manchester City"         "Manchester United"      
## [15] "Newcastle United"        "Nottingham Forest"      
## [17] "Sunderland"              "Tottenham Hotspur"      
## [19] "West Ham United"         "Wolverhampton Wanderers"

Modify factor order

Make two bar charts here - one before ordering another after

ggplot(data, aes(x = Team, y = Points)) +
geom_bar(stat = "identity", fill = "steelblue") +
coord_flip() +
labs(title = "Points by Team",
x = "Team",
y = "Points")

ggplot(data, aes(x = reorder(Team, Points), y = Points)) +
geom_bar(stat = "identity", fill = "steelblue") +
coord_flip() +
labs(title = "Points by Team",
x = "Team",
y = "Points")

Modify factor levels

Show examples of three functions:

fct_recode

data$Team <- factor(data$Team)


data$Team_recode <- fct_recode(data$Team,
                             "Man City" = "Manchester City",
                             "Man United" = "Manchester United",
                             "Spurs" = "Tottenham Hotspur")


head(data$Team_recode)
## [1] Arsenal                Man City               Chelsea               
## [4] Aston Villa            Brighton & Hove Albion Sunderland            
## 20 Levels: AFC Bournemouth Arsenal Aston Villa ... Wolverhampton Wanderers

fct_collapse

data$Team_grouped <- fct_collapse( data$Team, Top_Teams = c("Manchester City", "Arsenal", "Liverpool", "Chelsea"), Relegation_Zone = c("Norwich City", "Fulham", "Cardiff City"), Other = levels(data$Team)[!(levels(data$Team) %in% c("Manchester City","Arsenal","Liverpool","Chelsea", "Norwich City","Fulham","Cardiff City"))]
)
## Warning: Unknown levels in `f`: Norwich City, Cardiff City
table(data$Team_grouped)
## 
##           Other       Top_Teams Relegation_Zone 
##              15               4               1

fct_lump

data$Team_lumped <- fct_lump(data$Team, n = 5)

table(data$Team_lumped)
## 
##         AFC Bournemouth                 Arsenal             Aston Villa 
##                       1                       1                       1 
##               Brentford  Brighton & Hove Albion                 Burnley 
##                       1                       1                       1 
##                 Chelsea          Crystal Palace                 Everton 
##                       1                       1                       1 
##                  Fulham            Leeds United               Liverpool 
##                       1                       1                       1 
##         Manchester City       Manchester United        Newcastle United 
##                       1                       1                       1 
##       Nottingham Forest              Sunderland       Tottenham Hotspur 
##                       1                       1                       1 
##         West Ham United Wolverhampton Wanderers 
##                       1                       1