Skip to content

Instantly share code, notes, and snippets.

@apoorv74
Created June 29, 2018 13:10
Show Gist options
  • Save apoorv74/e4108b9ad9d3d14eb208dd15505be3b6 to your computer and use it in GitHub Desktop.
Save apoorv74/e4108b9ad9d3d14eb208dd15505be3b6 to your computer and use it in GitHub Desktop.
Plot player names as per the team formations over a football field (well, a green bg)
args <- commandArgs(trailingOnly = TRUE)
library(ggplot2)
library(ggflags)
bg_color = '#5A8E38'
# Declaring constants -----------------------------------------------------
theme_court = function(base_size = 16) {
theme_bw(base_size) +
theme(
text = element_text(color = "#A1BC6A"),
plot.background = element_rect(fill = bg_color, color = bg_color),
panel.background = element_rect(fill = bg_color, color = bg_color),
panel.grid = element_blank(),
panel.border = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks.length = unit(0, "lines"),
legend.background = element_rect(fill = bg_color, color = bg_color),
legend.position = "none",
legend.key = element_blank(),
legend.text = element_text(size = rel(1.0))
)
}
goal_keeper <- data.frame(loc_x = c(50), loc_y = c(90), formation = 'goalkeeper')
formation_442 <- data.frame(
loc_x = c(rep(quantile(seq(
10:90
))[2:5], 2), quantile(seq(10:90))[3] + 1, quantile(seq(10:90))[4] - 1),
loc_y = c(rep(77, 4), rep(67, 4), rep(57, 2)),
formation = rep('442', 10)
)
formation_433 <- data.frame(
loc_x = c(quantile(seq(10:90))[2:5], c(32,52,72) ,c(16,52,86)),
loc_y = c(rep(77, 4), 65,67,65, rep(57, 3)),
formation = rep('433', 10)
)
formation_4231 <- data.frame(
loc_x = c(quantile(seq(10:90))[2:5], quantile(seq(10:90))[3:4] ,c(25,52,75), 52),
loc_y = c(rep(77, 4), rep(67,2), c(60,62,60), 55),
formation = rep('4231', 10)
)
formation_4312 <- data.frame(
loc_x = c(quantile(seq(10:90))[2:5], c(36,52,66) ,52, c(39,63)),
loc_y = c(rep(77, 4), rep(67,3), c(60), c(55,55)),
formation = rep('4312', 10)
)
formation_4141 <- data.frame(
loc_x = c(quantile(seq(10:90))[2:5], 52, c(17,37,65,85) ,52),
loc_y = c(rep(77, 4), 70, rep(64, 4), 55),
formation = rep('4141', 10)
)
formation_352 <- data.frame(
loc_x = c(c(20,50,80), c(15,30,50,70,85) ,c(50,50)),
loc_y = c(rep(70, 3), 55,40,55,40,55, 30, 15),
formation = rep('352', 10)
)
all_formations <- dplyr::bind_rows(goal_keeper, formation_442,formation_433,formation_4231,formation_4312,formation_4141,formation_352)
# Create team formation ---------------------------------------------------
create_formation <- function(formation_a, formation_b = NA_character_){
team_a <- all_formations[all_formations$formation == formation_a,]
team_a <- dplyr::bind_rows(team_a, all_formations[all_formations$formation == 'goalkeeper',])
team_a$team <- 'team_a'
team_a$position <- c("Left-Back", "Centre-Back", "Right-Back", "Left Wing", "Left Midfield", "Defensive Midfield", "Right Midfield", "Right Wing", "Attacking Midfield", "Centre-Forward", "Keeper")
# subs <- c("Central Midfield", "Secondary Striker")
# team_b <- all_formations[all_formations$formation == formation_b,]
# team_b <- dplyr::bind_rows(team_b, all_formations[all_formations$formation == 'goalkeeper',])
# team_b$loc_y <- 100 - team_b$loc_y
# team_b$team <- 'team_b'
#
# formation_df <- dplyr::bind_rows(team_a, team_b)
# return(formation_df)
return(team_a)
}
# Create team plots -------------------------------------------------------
create_team_plot <- function(team_type, all_team){
formation_df <- create_formation('352')
if(team_type == 'y'){
team_young <- all_team[all_team$type == 'youngest',]
team_young$team_name <- c("Nigeria", "Mexico", "Morocco", "Serbia", "Nigeria", "Uruguay", "France", "Panama", "Australia","Morocco","Senegal","Argentina","Panama")
team_young$team_code <- c("ng", "mx", "ma", "rs", "ng", "uy", "fr", "pa", "au","ma","sn","ar","pa")
formation_df <- dplyr::left_join(formation_df, team_young, by='position')
} else {
team_senior <- all_team[all_team$type == 'senior_most',]
team_senior$team_name <- c("Egypt", "Mexico", "Australia", "Panama", "Russia", "Portugal", "Switzerland", "Japan", "Spain","Costa Rica","Colombia","Egypt","Poland")
team_senior$team_code <- c("eg", "mx", "au", "pa", "rs", "pt", "ch", "jp", "es","cr","co","eg","pl")
formation_df <- dplyr::left_join(formation_df, team_senior, by='position')
}
empty_court <- ggplot(data = formation_df,
aes(x = loc_x, y = loc_y,country=team_code,size=10,
label=name)) + geom_rect(aes(xmin = 0, xmax = 100, ymin = 0, ymax = 100), fill = NA, colour = "#000000", size = 1) +
# geom_rect(aes(xmin = 0, xmax = 100, ymin = 0, ymax = 50), fill = NA, colour = "#000000", size = 1) +
# geom_rect(aes(xmin = 21, xmax = 79, ymin = 17, ymax = 0), fill = NA, colour = "#000000", size = 1) +
geom_rect(aes(xmin = 21, xmax = 79, ymin = 83, ymax = 100), fill = NA, colour = "#000000", size = 1) +
# geom_rect(aes(xmin = 36.8, xmax = 63.2, ymin = 0, ymax = 6), fill = NA, colour = "#000000", size = 1) +
geom_rect(aes(xmin = 36.8, xmax = 63.2, ymin = 100, ymax = 94), fill = NA, colour = "#000000", size = 1) +
theme(rect = element_blank(),
line = element_blank(),
text = element_blank()) + theme_court()
# formation <- empty_court +
# geom_point(
# alpha = 0.8, size = 4
# ) + geom_text(colour = "#000000", aes(label=ifelse(loc_y < 50, formation, '')),hjust=0.5, vjust=-1.2,alpha = 0.8) +
# geom_point(
# alpha = 0.8, size = 4
# ) + geom_text(colour = "#000000", aes(label=ifelse(loc_y > 50, formation, '')),hjust=0.5, vjust=1.8,alpha = 0.8)
formation <- empty_court +
geom_flag() +
scale_country() +
geom_text(check_overlap = TRUE, aes(hjust=0.5, vjust=2)) + theme_court()
# browser()
# print(formation)
ggsave(
plot = formation,width = 6,height = 6,
filename = glue::glue("team_type_{team_type}.png"),
device = png(),
path = "/tmp"
)
}
# Get position wise young/old player info ---------------------------------
get_position_stats <- function(all_players, type){
all_players_data <<- read.csv(all_players)
# These players are present in the dataset but they did not participate in FIFA 2018
all_players_data <-
all_players_data[!all_players_data$name %in% c(
"Sebastian Szymanski",
"Hamza Mendyl",
"Noussair Mazraoui",
"Karim Hafez",
"Szymon Zurkowski",
"Ólafur Ingi Skúlason",
"Michael Krohn-Dehli",
"Farid Díaz"
),]
find_players <- function(playerPosition){
players <- all_players_data[all_players_data$position == playerPosition,]
youngest <- players[which(players$age == min(players$age)),]
seniorMost <- players[which(players$age == max(players$age)),]
return(list(youngest=youngest,seniorMost=seniorMost))
}
unique_positions <- unique(all_players_data$position)
x <- purrr::map(unique_positions, find_players)
youngest <- dplyr::bind_rows(lapply(x,"[[","youngest"))
youngest$type <- 'youngest'
senior_most <- dplyr::bind_rows(lapply(x,"[[","seniorMost"))
senior_most$type <- 'senior_most'
all_team <- dplyr::bind_rows(youngest, senior_most)
print(dim(all_team))
# write.csv(youngest,"/tmp/youngest_team.csv", row.names=F)
# write.csv(senior_most,"/tmp/senior_team.csv", row.names=F)
create_team_plot(type, all_team)
if(type == 'y'){
return(youngest)
} else if(type == 'o'){
return(senior_most)
} else {
print("Enter [y]oung/[o]ld")
}
}
# Calling the function ----------------------------------------------------
# get_position_stats(all_players = "/tmp/all_players_age.csv", type = "o")
get_position_stats(all_players = args[1], type = args[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment