rm(list = ls())
library(tidyverse)
df_tidy <- read_csv("C:\\Users\\Thinh Dao\\Desktop\\Mini R Project 7.7.19\\Fifa 19\\Practice#1\\fifa19_tidy.scv") #load the data
df_tidy %>%
select(Player_name, Club, League, OVR, POT, POS, GROW, Age, Foot) -> df_tidy # Select some columns of the datat set
df_tidy %>%
mutate(POS_group = case_when(POS %in% c("CB", "LB", "RB", "LWB", "RWB") ~ "DEF",
POS %in% c("CM", "CDM", "CAM", "LM", "RM") ~ "MID",
POS == "GK" ~ "GK",
TRUE ~ "ATT")) %>%
mutate(Age_group = case_when(Age < 19 ~ "U19",
Age >= 19 & Age < 23 ~ "A19-U23",
Age >=23 & Age < 27 ~ "A23-U27",
Age >=27 & Age < 31 ~ "A27-U31",
Age >=31 & Age < 35 ~ "A31-U35",
TRUE ~ "O35")) %>%
mutate(Player_pos = paste0(Player_name, " ", "(", POS, ")")) -> df_tidy # Classify Position group and age group
df_tidy[!(df_tidy$POT %>% is.na()),] -> df_plot #Remove the NA in POT columns
# Plotting the most potential players by position:
df_plot %>%
group_by(POS) %>%
filter(POT == max(POT)) %>%
ungroup() -> df_segment1
df_segment1 %>%
ggplot(aes(y = reorder(Player_pos, POT)))+
geom_segment(data = df_segment1,
aes(x = OVR, xend = POT, yend = Player_pos),
size = 1,
color = "lightslategrey") +
geom_segment(data = df_segment1,
aes(x = 72, xend = OVR, yend = Player_pos),
linetype = "dotted",
size = 0.5,
color = "lightslategrey") +
geom_point(data = df_segment1, aes(x = OVR, color = "color1"), size =5, alpha = 1)+
geom_point(data = df_segment1, aes(x = POT, color = "color2"), size =5, alpha = 1)+
geom_text(data = df_segment1 %>% filter(GROW != 0), aes(x= OVR, label = OVR), size = 5.5, hjust = 1.4, color = "grey20") +
geom_text(data = df_segment1 ,
aes(x = POT, y = Player_pos, label = POT),
size = 5.5, hjust = - 0.4, color = "grey20") +
theme_minimal() +
theme(plot.background = element_rect(fill = "whitesmoke", linetype = 0))+
theme(panel.grid = element_blank())+
scale_x_continuous(limits = c(72, 97),
expand = c(0 , 0)) +
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.line.x = element_blank()) +
theme(axis.text.y = element_text(color = "grey20", size = 15),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.y = element_blank())+
scale_color_manual(name="", values= c("color1" = "#ff7f00", "color2" = "#4daf4a"),
labels = c("Current Overall Score", "Potential Score"))+
theme(legend.title = element_blank(),
legend.text = element_text(face = "italic", color = "grey20", size = 15),
legend.position = c(0.83, 0.25))+
theme(plot.margin = unit(c(1.2, 1.2, 1.2, 1.2), "cm")) +
theme(plot.title = element_text(size = 29, color = "gray20")) +
theme(plot.subtitle = element_text(size = 17, color = "gray20", face ="italic" )) +
theme(plot.caption = element_text(size = 15, color = "gray20", face = "italic"))+
labs(x = NULL, y = NULL,
title = "Fifa19: The most potential players by positions" ,
subtitle = "Therer are 15 positions in total including 4 positions of Attacker \n 5 positions of Midfielder, 5 positions of Defender and Goalkeeper position",
caption = "Data Source: https://www.futwiz.com/en/fifa19/career-mode")+
annotate("text", label = "*",
x = 94.6,
y = 21.2,
color = "firebrick",
size = 7, hjust = 0, vjust = 1)+
annotate("text", label = "*",
x = 92.6,
y = 15.2,
color = "firebrick",
size = 7, hjust = 0, vjust = 1)+
annotate("text", label = "*",
x = 91.6,
y = 12.2,
color = "firebrick",
size = 7, hjust = 0, vjust = 1)+
annotate("text", label = "* The player who already reached his limit ",
x = 90.7,
y = 5.5,
color = "firebrick",
size = 5, hjust = 0, vjust = 1)
df_plot %>%
group_by(POS_group) %>%
top_n(10, wt = POT) %>%
ungroup() %>%
arrange(POS_group, -POT) -> df_segment2
my_label <- c("ATT" = "ATTACKER", "MID" = "MIDFIELDER", "DEF" = "DEFENDER", "GK" = "GOALKEEPER")
df_segment2$POS_group <- factor(df_segment2$POS_group, levels = c("ATT", "MID", "DEF", "GK"))
df_segment2 %>%
ggplot(aes(y = reorder(Player_pos,POT)))+
geom_segment(data = df_segment2,
aes(x = OVR, xend = POT, yend = Player_pos),
size = 1,
color = "lightslategrey")+
geom_segment(data = df_segment2,
aes(x = 71.5, xend = OVR, yend = Player_pos),
linetype = "dotted",
size = 0.5,
color = "lightslategrey")+
geom_point(data = df_segment2, aes(x = OVR, color = "color1"), size =5, alpha = 1)+
geom_point(data = df_segment2, aes(x = POT, color = "color2"), size =5, alpha = 1)+
geom_text(data = df_segment2 %>% filter(GROW != 0),
aes(x= OVR, y = Player_pos, label = OVR),
size = 5.5, hjust = 1.4, color = "grey20") +
geom_text(data = df_segment2,
aes(x = POT, y = Player_pos, label = POT),
size = 5.5, hjust = - 0.4, color = "grey20") +
theme_minimal() +
theme(plot.background = element_rect(fill = "whitesmoke", linetype = 0))+
theme(panel.grid = element_blank())+
scale_x_continuous(limits = c(71.5, 97), expand = c(0, 0)) +
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.line.x = element_blank()
) +
theme(axis.text.y = element_text(color = "grey20", size = 15),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.y = element_blank()
)+
facet_wrap(~ POS_group, scales = "free",
labeller = labeller(POS_group = my_label),
ncol = 2) +
theme(strip.text = element_text(face = "bold", color = "grey20", size = 15)
)+
scale_color_manual(name="", values= c("color1" = "#ff7f00", "color2" = "#4daf4a"),
labels = c("Current Overall Score", "Potential Score"))+
theme(legend.title = element_blank(),
legend.text = element_text(face = "bold.italic", color = "grey20", size = 15),
legend.position = "bottom")+
theme(plot.margin = unit(c(1.2, 1.2, 1.2, 1.2), "cm")) +
theme(plot.title = element_text(size = 29, color = "gray20")) +
theme(plot.subtitle = element_text(size = 17, color = "gray20", face ="italic" )) +
theme(plot.caption = element_text(size = 15, color = "gray20", face = "italic"))+
labs(x = NULL, y = NULL,
#subtitle = "Attacker: ST, CF, LW, RW\nMidfielder: CM, CAM, CDM, RM, LM\nDefender: CB, LB, RB, LWB, RWB\nGoalkeeper: GK",
title = "Fifa19: The most potential players by position groups" ,
caption = "Data Source: https://www.futwiz.com/en/fifa19/career-mode")+
theme(panel.spacing = unit(1.5, "line"))
df_plot %>%
filter(Age_group == "U19" | Age_group == "A19-U23") %>%
group_by(Age_group) %>%
top_n(10, wt = POT) %>%
ungroup() %>%
arrange(Age_group, -POT) -> df_segment3
df_segment3$Age_group %>% unique()
my_label1 <- c("U19" = "Under 19",
"A19-U23" = "From 19 to under 23")
df_segment3$Age_group <- factor(df_segment3$Age_group,
levels = c("U19", "A19-U23"))
df_segment3 %>%
ggplot(aes(y = reorder(Player_pos,POT)))+
geom_segment(data = df_segment3,
aes(x = OVR, xend = POT, yend = Player_pos),
size = 1,
color = "lightslategrey")+
geom_segment(data = df_segment3,
aes(x = 63.5, xend = OVR, yend = Player_pos),
linetype = "dotted",
size = 0.5,
color = "lightslategrey")+
geom_point(data = df_segment3,
aes(x = OVR, color = "color1"), size =5, alpha = 1)+
geom_point(data = df_segment3,
aes(x = POT, color = "color2"), size =5, alpha = 1)+
geom_text(data = df_segment3 %>% filter(GROW != 0),
aes(x= OVR, y = Player_pos, label = OVR),
size = 5.5, hjust = 1.4, color = "grey20") +
geom_text(data = df_segment3,
aes(x = POT, y = Player_pos, label = POT),
size = 5.5, hjust = - 0.4, color = "grey20") +
theme_minimal() +
theme(plot.background = element_rect(fill = "whitesmoke",linetype = 0))+
theme(panel.grid = element_blank())+
scale_x_continuous(limits = c(63.5, 97),
expand = c(0,0)) +
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.line.x = element_blank()
) +
theme(axis.text.y = element_text(color = "grey20", size = 15),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.y = element_blank()
)+
facet_wrap(~ Age_group, scales = "free",
labeller = labeller(Age_group = my_label1),
ncol = 2) +
theme(strip.text = element_text(face = "bold", color = "grey20", size = 15)
)+
scale_color_manual(name="", values= c("color1" = "#ff7f00", "color2" = "#4daf4a"),
labels = c("Current Overall Score", "Potential Score"))+
theme(legend.title = element_blank(),
legend.text = element_text(face = "bold.italic", color = "grey20", size = 15),
legend.position = "top")+
theme(plot.margin = unit(c(1.2, 1.2, 0.5, 1.2), "cm")) +
theme(plot.title = element_text(size = 29, color = "gray20")) +
theme(plot.subtitle = element_text(size = 17, color = "gray20", face ="italic" )) +
theme(plot.caption = element_text(size = 15, color = "gray20", face = "italic"))+
labs(x = NULL, y = NULL,
subtitle = "The age groups are defined as under 19 (U19), from 19 to under 23 (A19-U23) and so on" ,
title = "Fifa19: The most potential players by age groups") -> g3
df_plot %>%
filter(Age_group == "A23-U27" | Age_group == "A27-U31") %>%
group_by(Age_group) %>%
top_n(10, wt = POT) %>%
ungroup() %>%
arrange(Age_group, -POT) -> df_segment4
my_label2 <- c("A23-U27" = "From 23 to under 27",
"A27-U31" = "From 27 to under 31")
df_segment4$Age_group <- factor(df_segment4$Age_group,
levels = c("A23-U27", "A27-U31"))
df_segment4 %>%
ggplot(aes(y = reorder(Player_pos,POT)))+
geom_segment(data = df_segment4,
aes(x = OVR, xend = POT, yend = Player_pos),
size = 1,
color = "lightslategrey")+
geom_segment(data = df_segment4,
aes(x = 63.5, xend = OVR, yend = Player_pos),
linetype = "dotted",
size = 0.5,
color = "lightslategrey")+
geom_point(data = df_segment4,
aes(x = OVR, color = "color1"), size =5, alpha = 1)+
geom_point(data = df_segment4,
aes(x = POT, color = "color2"), size =5, alpha = 1)+
geom_text(data = df_segment4 %>% filter(GROW != 0),
aes(x= OVR, y = Player_pos, label = OVR),
size = 5.5, hjust = 1.4, color = "grey20") +
geom_text(data = df_segment4,
aes(x = POT, y = Player_pos, label = POT),
size = 5.5, hjust = - 0.4, color = "grey20") +
theme_minimal() +
theme(plot.background = element_rect(fill = "whitesmoke", linetype = 0))+
theme(panel.grid = element_blank(),
panel.border = element_blank())+
scale_x_continuous(limits = c(63.5, 97),
expand = c(0,0)) +
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.line.x = element_blank()) +
theme(axis.text.y = element_text(color = "grey20", size = 15),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.y = element_blank())+
facet_wrap(~ Age_group, scales = "free",
labeller = labeller(Age_group = my_label2),
ncol = 2) +
theme(strip.text = element_text(face = "bold", color = "grey20", size = 15))+
scale_color_manual(values= c("color1" = "#ff7f00", "color2" = "#4daf4a"))+
theme(legend.title = element_blank(),
legend.text = element_blank(),
legend.position = "none")+
theme(plot.margin = unit(c(0, 1.2, 1.2, 1.2), "cm")) +
theme(plot.caption = element_text(size = 15, color = "gray20", face = "italic"))+
labs(x = NULL, y = NULL,
caption = "Data Source: https://www.futwiz.com/en/fifa19/career-mode") -> g4
library(cowplot)
plot_grid(g3, g4, nrow = 2, rel_heights = c(1.65, 1))