options(scipen=999)
library(tidyverse)
library(socviz)
library(datasetsICR)
library(ggplot2)LAB 5
Insights from FIFA Dataset
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
PART 1: PRACTICE USING PIPES (dplyr) TO SUMMARIZE DATA: TWO CATEGORICAL VARIABLES
nationality_counts <- FIFA %>%
na.omit() %>%
group_by(Nationality) %>%
summarize(total_count = n()) %>%
arrange(desc(total_count))
top_10_nationalities <- head(nationality_counts$Nationality, 10)
filtered_FIFA <- FIFA %>%
na.omit() %>%
filter(Nationality %in% top_10_nationalities)
pip1 <- filtered_FIFA %>%
group_by(Nationality, Preferred.Foot) %>%
summarize(N = n()) %>%
mutate(freq = N/sum(N),
pct = round((freq * 100), 0))
pip1# A tibble: 20 × 5
# Groups: Nationality [10]
Nationality Preferred.Foot N freq pct
<fct> <fct> <int> <dbl> <dbl>
1 Argentina Left 214 0.229 23
2 Argentina Right 722 0.771 77
3 Brazil Left 245 0.297 30
4 Brazil Right 580 0.703 70
5 Colombia Left 135 0.219 22
6 Colombia Right 481 0.781 78
7 England Left 386 0.233 23
8 England Right 1271 0.767 77
9 France Left 236 0.259 26
10 France Right 675 0.741 74
11 Germany Left 274 0.229 23
12 Germany Right 921 0.771 77
13 Italy Left 189 0.270 27
14 Italy Right 510 0.730 73
15 Japan Left 83 0.174 17
16 Japan Right 395 0.826 83
17 Netherlands Left 109 0.241 24
18 Netherlands Right 343 0.759 76
19 Spain Left 298 0.278 28
20 Spain Right 773 0.722 72
p <- ggplot(data = subset(pip1, !is.na(Nationality) & !is.na(Preferred.Foot)), aes(x = Nationality, y = pct, fill = Preferred.Foot)) +
geom_col(position = "stack") +
labs(x = "Nationality", y = "Percent", fill = "Preferred.Foot")
p + geom_text(aes(label = paste0(pct, "%")), position = position_stack(vjust = 0.5))This output shows the ten nations that contribute the most players to FIFA and the percentage of their preferred foot. At first glance, the data does make sense. Every nation shows a majority of players that prefer their right foot. Brazil has the highest percentage of players that prefer their left foot and Japan has the highest percentage of players that prefer their right.
PART 2: CREATE STACKED AND DODGED BAR CHARTS FROM 2 CATEGORICAL VARIABLES
# AS DODGED BAR CHART
ggplot(data = subset(pip1, !is.na(Nationality) & !is.na(Preferred.Foot)),
aes(x = Nationality, y = pct, fill = Preferred.Foot)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Preferred Foot by Nationality",
x = "Nationality", y = "Percentage", fill = "Preferred Foot",
caption = "FIFA Dataset") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(subtitle = "Dodged Bar Chart") +
scale_fill_manual(values = c("seagreen", "skyblue"))# AS STACKED BAR CHART
ggplot(data = subset(pip1, !is.na(Nationality) & !is.na(Preferred.Foot)),
aes(x = Nationality, y = pct, fill = Preferred.Foot)) +
geom_bar(stat = "identity", position = "stack") +
labs(title = "Preferred Foot by Nationality",
x = "Nationality", y = "Percentage", fill = "Preferred Foot",
caption = "FIFA Dataset") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(subtitle = "Stacked Bar Chart") +
scale_fill_manual(values = c("seagreen", "skyblue"))# AS FACETED HORIZONTAL BAR CHART
ggplot(data = subset(pip1, !is.na(Nationality) & !is.na(Preferred.Foot)),
aes(x = pct, y = Nationality, fill = Preferred.Foot)) +
geom_bar(stat = "identity") +
labs(title = "Preferred Foot by Nationality",
x = "Percentage", y = "Nationality",
caption = "FIFA Dataset") +
theme(axis.text.y = element_text(hjust = 1),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
labs(subtitle = "Faceted Horizontal Bar Chart") +
facet_grid(~Preferred.Foot, scales = "free_y", space = "free_y") +
guides(fill = "none") +
scale_fill_manual(values = c("seagreen", "skyblue"))PART 3: PRACTICE USING PIPES (dplyr) TO SUMMARIZE DATA: TWO CONTINUOUS & ONE CATEGORICAL VARIABLE
filtered_FIFA$Wage <- as.numeric(filtered_FIFA$Wage)
pip2 <- filtered_FIFA %>%
group_by(Nationality) %>%
summarize(N = n(),
wage_mean = mean(Wage, na.rm=TRUE),
height_mean = mean(Height, na.rm=TRUE)) %>%
mutate(freq = N/sum(N),
pct = round((freq*100),0))
pip2# A tibble: 10 × 6
Nationality N wage_mean height_mean freq pct
<fct> <int> <dbl> <dbl> <dbl> <dbl>
1 Argentina 936 11.5 179. 0.106 11
2 Brazil 825 17.8 181. 0.0933 9
3 Colombia 616 5.43 179. 0.0697 7
4 England 1657 9.97 182. 0.187 19
5 France 911 14.1 182. 0.103 10
6 Germany 1195 9.66 184. 0.135 14
7 Italy 699 13.5 183. 0.0791 8
8 Japan 478 3.45 178. 0.0541 5
9 Netherlands 452 10.1 183. 0.0511 5
10 Spain 1071 16.3 181. 0.121 12
By using the two continuous (Wage and Height) and one categorical (Nationality), further analyses can be observed. The outputs make sense, as each of the Nationalities shows the average Wage and the average Height. Brazil has the highest average wage and a mediocre height. Japan has the lowest average wage and the lowest average height.
PART 4: SCATTERPLOT WITH A THIRD CATEGORICAL VARIABLE
This code does not remove NA – make sure you deal with that.
p <- ggplot(pip2, aes(x = wage_mean, y = height_mean, color = Nationality)) +
geom_point(size = 5) +
annotate(geom = "text", x = 1.6, y = 176,
label = "Japan shows the lowest average height as well as lowest average wage.", hjust = 0) +
labs(y = "Average Height", x = "Average Wage.",
title = "Wage and Height by Nationality",
subtitle = "Correlation between height and wage",
caption = "source(datasetsICR)") +
scale_y_continuous(limits = c(175, 185))
pPART 5: LEGEND AND GUIDES
This is just an example – do not simply duplicate these options.
pip2$Nationality.c <- as.character(pip2$Nationality)
pip2 <- pip2[order(pip2$Nationality.c),]
pip2# A tibble: 10 × 7
Nationality N wage_mean height_mean freq pct Nationality.c
<fct> <int> <dbl> <dbl> <dbl> <dbl> <chr>
1 Argentina 936 11.5 179. 0.106 11 Argentina
2 Brazil 825 17.8 181. 0.0933 9 Brazil
3 Colombia 616 5.43 179. 0.0697 7 Colombia
4 England 1657 9.97 182. 0.187 19 England
5 France 911 14.1 182. 0.103 10 France
6 Germany 1195 9.66 184. 0.135 14 Germany
7 Italy 699 13.5 183. 0.0791 8 Italy
8 Japan 478 3.45 178. 0.0541 5 Japan
9 Netherlands 452 10.1 183. 0.0511 5 Netherlands
10 Spain 1071 16.3 181. 0.121 12 Spain
p <- ggplot(pip2, aes(x=wage_mean, y=height_mean, color=Nationality.c))
p + geom_point(size=4) +
annotate(geom = "text", x = 1.6, y = 176,
label = "The lower average heights have lower wages while the higher average heights have higher wages.", hjust = 0, size = 3) +
labs(y = "Average Height", x = "Average Wage.",
title = "Wage and Height by Nationality",
subtitle = "Correlation between height and wage",
caption = "source(datasetsICR)") +
scale_y_continuous(limits = c(175, 185)) +
theme(legend.title = element_text(color = "gray50", size = 4, face = "bold"),
legend.text = element_text(size = 5),
legend.position = c(.07, 0.59))PART 6: DATA LABELS VS LEGEND
p <- ggplot(pip2, aes(x=wage_mean, y=height_mean, color=Nationality.c))
p + geom_point(size=4) +
geom_text(mapping = aes(label=Nationality), hjust=1.2, size=3) +
annotate(geom = "text", x = 1.6, y=58,
label = "Example data point note.\nYou need acutally say something interesting here.", hjust=0) +
labs(y = "Average Height", x = "Average Wage.",
title = "Wage and Height by Nationality",
subtitle = "Correlation between height and wage",
caption = "source(datasetsICR)") +
scale_y_continuous(limits = c(175, 185)) +
theme(legend.position = "none")PART 7: INTERPRETATION
The scatter plot shows slight correlation between height and wage. Japan has the lowest average height as well as the lowest average wage. From there, the countries go up in both height and wage. While Germany has the highest average height, their average wage is mediocre. Adding more of the countries that were excluded, further insights could be made. It would also be helpful to take out all of the outliers in each country to see if there is higher correlation.