LAB 5

Author

Elsie Sorrell

options(scipen=999) 
library(tidyverse)
library(socviz)
library(datasetsICR)
data(FIFA)
head(FIFA)
               Name Age Nationality Overall Potential                Club Value
1          L. Messi  31   Argentina      94        94        FC Barcelona 110.5
2 Cristiano Ronaldo  33    Portugal      94        94            Juventus    77
3         Neymar Jr  26      Brazil      92        93 Paris Saint-Germain 118.5
4            De Gea  27       Spain      91        93   Manchester United    72
5      K. De Bruyne  27     Belgium      91        92     Manchester City   102
6         E. Hazard  27     Belgium      91        91             Chelsea    93
  Wage Special Preferred.Foot International.Reputation Weak.Foot Skill.Moves
1  565    2202           Left                        5         4           4
2  405    2228          Right                        5         4           5
3  290    2143          Right                        5         5           5
4  260    1471          Right                        4         3           1
5  355    2281          Right                        4         5           4
6  340    2142          Right                        4         4           4
       Work.Rate Position Jersey.Number Contract.Valid.Until Height   Weight
1 Medium/ Medium       RF            10                 2021 170.18 72.12119
2      High/ Low       ST             7                 2022 187.96 83.00740
3   High/ Medium       LW            10                 2022 175.26 68.03886
4 Medium/ Medium       GK             1                 2020 193.04 76.20352
5     High/ High      RCM             7                 2023 180.34 69.85322
6   High/ Medium       LF            10                 2020 172.72 73.93556
    LS   ST   RS   LW   LF   CF   RF   RW  LAM  CAM  RAM   LM  LCM   CM  RCM
1 88+2 88+2 88+2 92+2 93+2 93+2 93+2 92+2 93+2 93+2 93+2 91+2 84+2 84+2 84+2
2 91+3 91+3 91+3 89+3 90+3 90+3 90+3 89+3 88+3 88+3 88+3 88+3 81+3 81+3 81+3
3 84+3 84+3 84+3 89+3 89+3 89+3 89+3 89+3 89+3 89+3 89+3 88+3 81+3 81+3 81+3
4                                                                           
5 82+3 82+3 82+3 87+3 87+3 87+3 87+3 87+3 88+3 88+3 88+3 88+3 87+3 87+3 87+3
6 83+3 83+3 83+3 89+3 88+3 88+3 88+3 89+3 89+3 89+3 89+3 89+3 82+3 82+3 82+3
    RM  LWB  LDM  CDM  RDM  RWB   LB  LCB   CB  RCB   RB Crossing Finishing
1 91+2 64+2 61+2 61+2 61+2 64+2 59+2 47+2 47+2 47+2 59+2       84        95
2 88+3 65+3 61+3 61+3 61+3 65+3 61+3 53+3 53+3 53+3 61+3       84        94
3 88+3 65+3 60+3 60+3 60+3 65+3 60+3 47+3 47+3 47+3 60+3       79        87
4                                                              17        13
5 88+3 77+3 77+3 77+3 77+3 77+3 73+3 66+3 66+3 66+3 73+3       93        82
6 89+3 66+3 63+3 63+3 63+3 66+3 60+3 49+3 49+3 49+3 60+3       81        84
  HeadingAccuracy ShortPassing Volleys Dribbling Curve FKAccuracy LongPassing
1              70           90      86        97    93         94          87
2              89           81      87        88    81         76          77
3              62           84      84        96    88         87          78
4              21           50      13        18    21         19          51
5              55           92      82        86    85         83          91
6              61           89      80        95    83         79          83
  BallControl Acceleration SprintSpeed Agility Reactions Balance ShotPower
1          96           91          86      91        95      95        85
2          94           89          91      87        96      70        95
3          95           94          90      96        94      84        80
4          42           57          58      60        90      43        31
5          91           78          76      79        91      77        91
6          94           94          88      95        90      94        82
  Jumping Stamina Strength LongShots Aggression Interceptions Positioning
1      68      72       59        94         48            22          94
2      95      88       79        93         63            29          95
3      61      81       49        82         56            36          89
4      67      43       64        12         38            30          12
5      63      90       75        91         76            61          87
6      56      83       66        80         54            41          87
  Vision Penalties Composure Marking StandingTackle SlidingTackle GKDiving
1     94        75        96      33             28            26        6
2     82        85        95      28             31            23        7
3     87        81        94      27             24            33        9
4     68        40        68      15             21            13       90
5     94        79        88      68             58            51       15
6     89        86        91      34             27            22       11
  GKHandling GKKicking GKPositioning GKReflexes Release.Clause
1         11        15            14          8          226.5
2         11        15            14         11          127.1
3          9        15            15         11          228.1
4         85        87            88         94          138.6
5         13         5            10         13          196.4
6         12         6             8          8          172.1
# potential and age
# Work.Rate and position

READ THIS DISCLAIMER THIS QUARTO FILE OFFERS A BASIC OUTLINE FOR THE REPORT AND EXAMPLE FOR SOME OF THE CODE.
THIS FILE IS NOT COMPLETE AND NOT INTENDED FOR YOU TO SIMPLY REPLICATE.
YOU WILL NEED TO WORK THROUGH ALL REQUIREMENTS ON YOUR OWN, INCLUDING INTERPRETATIONS, AESTHETICS, LABELS, COLORS, TITLES, SUBTITLES, ETC.
BE CREATIVE!


PART 0: Useful code ONLY for those assigned the customers{datasetsICR}

Skip (DELETE) this if you’re using the gss_sm dataset

Note, if you are using the customers dataset, then you will need to transform the Channel and Region from integer into factor variables, using text for the levels. The data dictionary for the dataset provides the factor values. Here is some code for you to accomplish this task :-).

# Collapse positions
FIFA <- FIFA %>%
  mutate(Position = as.factor(Position) %>% recode_factor(
    "GK" = "Goalkeeper",
    "RB" = "Defender",
    "LB" = "Defender",
    "CB" = "Defender",
    "RWB" = "Defender",
    "LWB" = "Defender",
    "LCB" = "Defender",
    "RCB" = "Defender",
    "CDM" = "Midfielder",
    "CM" = "Midfielder",
    "CAM" = "Midfielder",
    "RM" = "Midfielder",
    "LM" = "Midfielder",
    "RAM" = "Midfielder",
    "LCM" = "Midfielder",
    "LAM" = "Midfielder",
    "LDM" = "Midfielder",
    "RCM" = "Midfielder",
    "RDM" = "Midfielder",
    "RW" = "Forward",
    "LW" = "Forward",
    "CF" = "Forward",
    "RF" = "Forward",
    "LF" = "Forward",
    "LS" = "Forward",
    "RS" = "Forward",
    "ST" = "Forward"
  ))

head(FIFA$Position)
[1] Forward    Forward    Forward    Goalkeeper Midfielder Forward   
Levels: Goalkeeper Defender Midfielder Forward 

PART 1: PRACTICE USING PIPES (dplyr) TO SUMMARIZE DATA: TWO CATEGORICAL VARIABLES

library(socviz)
pip1 <- FIFA %>%         
  group_by(Position, Work.Rate) %>%
  summarize(N = n()) %>% 
  mutate(freq = N/sum(N),
         pct = round((freq*100),0)) %>%
  filter(Position != "")

pip1
# A tibble: 27 × 5
# Groups:   Position [4]
   Position   Work.Rate          N     freq   pct
   <fct>      <fct>          <int>    <dbl> <dbl>
 1 Goalkeeper Medium/ Medium  2025 1          100
 2 Defender   High/ High       254 0.0433       4
 3 Defender   High/ Low         41 0.00699      1
 4 Defender   High/ Medium    1045 0.178       18
 5 Defender   Low/ High        341 0.0581       6
 6 Defender   Low/ Low           3 0.000511     0
 7 Defender   Low/ Medium      362 0.0617       6
 8 Defender   Medium/ High     868 0.148       15
 9 Defender   Medium/ Low       21 0.00358      0
10 Defender   Medium/ Medium  2931 0.500       50
# ℹ 17 more rows

Interpretation

The data does make sense. I subset all players into four general categories, midfielder, forward, goal keeper, and defender.

PART 2: CREATE STACKED AND DODGED BAR CHARTS FROM 2 CATEGORICAL VARIABLES

Position <- pip1 %>%
  filter(Position != "")
p_title <- "Position by Work Rate"
p_caption <- "ICRdatasets, FIFA"

# AS STACKED BAR CHART
p <- ggplot(data = subset(pip1, !is.na(Position) & !is.na(Work.Rate)), 
                        aes(x=Position, y=pct, fill = Work.Rate))

p + geom_col(position = "stack") +
    labs(x="Position", y="Percent", fill = "Work Rate",
         title = p_title, caption = p_caption, 
         subtitle = "As a stacked bar chart") +
    geom_text(aes(label=pct), position = position_stack(vjust=.5))

# AS DODGED BAR CHART
p + geom_col(position = "dodge2") +
    labs(x="Position", y="Percent", fill = "Work Rate",
         title = p_title, caption = p_caption, 
         subtitle = "As a dodged bar chart") + 
    geom_text(aes(label = pct), position = position_dodge(width = .9)) 

# AS FACETED HORIZONTAL BAR CHART
p + geom_col(position = "dodge2") +
    labs(x=NULL, y="Percent", fill = "Work.Rate",
         title = p_title, caption = p_caption, 
         subtitle = "As a faceted horizontal bar chart") +
         guides(fill = "none") +
         coord_flip() +
         facet_grid(~ Position) +
    geom_text(aes(label = pct), position = position_dodge2(width = 1))

PART 3: PRACTICE USING PIPES (dplyr) TO SUMMARIZE DATA: TWO CONTINUOUS & ONE CATEGORICAL VARIABLE

pip2 <- FIFA %>%         
  group_by(Position) %>%
  summarize(N = n(),
            age_mean = mean(Age, na.rm=TRUE), 
            potential_mean = mean(Potential, na.rm=TRUE)) %>% 
  mutate(freq = N/sum(N),
         pct = round((freq*100),0)) %>% 
  filter(Position != "")
pip2
# A tibble: 4 × 6
  Position       N age_mean potential_mean  freq   pct
  <fct>      <int>    <dbl>          <dbl> <dbl> <dbl>
1 Goalkeeper  2025     26.0           69.8 0.111    11
2 Defender    5866     25.5           71.0 0.322    32
3 Midfielder  6838     24.8           71.8 0.376    38
4 Forward     3418     24.7           71.8 0.188    19

Interpretation

The older average players have a lower potential value. This does make sense because as players get older the less likely they are to drastically improve in skill.

PART 4: SCATTERPLOT WITH A THIRD CATEGORICAL VARIABLE

This code does not remove NA – make sure you deal with that.
::: {.cell}

pip3 <- pip2[complete.cases(pip2$age_mean, pip2$potential_mean),]
p <- ggplot(pip3, aes(x = age_mean, y = potential_mean, color = Position))
p + geom_point(size=8) +
    annotate(geom = "text", x = 2.0, y=60, 
                     label = "The positions midfielder and forward are overlapping each other.", hjust=0) +
    labs(y="Average Potential Score", x="Average Age.", 
         title="Age and Potential of Player by Position", 
         subtitle = "Goalkeeper has the lowest Potential Score",
         caption <- "FIFA, ICRdatasets")

:::

PART 5: LEGEND AND GUIDES

This is just an example – do not simply duplicate these options.
::: {.cell}

pip3 <- pip2[complete.cases(pip2$age_mean, pip2$potential_mean),]
p <- ggplot(pip3, aes(x = age_mean, y = potential_mean, color = Position))
p + geom_point(size=8) +
    annotate(geom = "text", x = 24, y=70, 
                     label = "The positions midfielder and forward are closest to each other.", hjust=0) +
    labs(y="Average Potential Score", x="Average Age.", 
         title="Age and Potential of Player by Position", 
         subtitle = "Goalkeeper has the lowest Potential Score",
         caption <- "FIFA, ICRdatasets") +
  theme(legend.title = element_text(color="black", size=15, face="italic"),
        legend.position = "top") 

:::

PART 6: DATA LABELS VS LEGEND

p <- ggplot(pip3, aes(x=age_mean, y=potential_mean, color=Position))
p + geom_point(size=7) +
    geom_text(mapping = aes(label=Position), hjust=1.3, size=3.5) +
    annotate(geom = "text", x = 24, y=70, 
                     label = "Forward has the lowest average age and highest potential score.", hjust=0) +
    labs(y="Average Potential Score", x="Average Age", 
         title="Age and Potential of Player by Position", 
         color = "Position") +
    theme(legend.position = "none")

PART 7: INTERPRETATION

Forwards and midfielders are the youngest average players, which correlates with these players having a higher potential score. Defenders are about 25.5 years old with a potential score of 71.0. Lastly, Goalkeepers have the highest average age of 26, and the lowest potential score just under 70.
Summarize those insights as the subtitle.

p <- ggplot(pip3, aes(x=age_mean, y=potential_mean, color=Position))
p + geom_point(size=7) +
    geom_text(mapping = aes(label=Position), hjust=1.3, size=3.5) +
    annotate(geom = "text", x = 24, y=70, 
                     label = "Forward has the lowest average age and highest potential score.", hjust=0) +
    labs(y="Average Potential Score", x="Average Age", 
         title="Age and Potential of Player by Position",
         subtitle = "Forwards and midfielders are younger with higher potential scores than goalkeepers and defenders",
         color = "Position") +
    theme(legend.position = "none")

END