From 542602ab3adb29147e7e788f67b2bb93963a8023 Mon Sep 17 00:00:00 2001 From: Tony ElHabr Date: Sun, 21 Jan 2024 08:52:46 -0600 Subject: [PATCH] devtools check all the things --- NAMESPACE | 2 +- R/data.R | 30 ++++++++++++++++++++++++++++ R/geom_soccer_logos.R | 34 ++++++++++++++++---------------- R/scales.R | 9 +++------ R/utils.R | 28 +++++++++++++++++++-------- man/geom_soccer_logos.Rd | 33 ++++++++++++++++--------------- man/scale_soccer.Rd | 11 +++-------- man/team_name_mapping.Rd | 42 ++++++++++++++++++++++++++++++++++++++++ man/valid_names.Rd | 25 ++++++++++++++++++++++++ man/valid_team_names.Rd | 17 ---------------- 10 files changed, 158 insertions(+), 73 deletions(-) create mode 100644 R/data.R create mode 100644 man/team_name_mapping.Rd create mode 100644 man/valid_names.Rd delete mode 100644 man/valid_team_names.Rd diff --git a/NAMESPACE b/NAMESPACE index ac25a44..725d9b1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(GeomSoccerlogo) +export(all_valid_team_names) export(clean_team_names) export(geom_soccer_logos) export(scale_color_soccer) @@ -11,4 +12,3 @@ import(ggpath) import(grid) importFrom(cli,cli_warn) importFrom(ggplot2,element_grob) -importFrom(stats,setNames) diff --git a/R/data.R b/R/data.R new file mode 100644 index 0000000..f422695 --- /dev/null +++ b/R/data.R @@ -0,0 +1,30 @@ +#' Alternate Team Abbreviation Mapping +#' +#' A list of named character vectors mapping common alternate team abbreviations. +#' +#' You can suggest additions to this table by +#' [opening an issue in soccerplotR](https://github.com/tonyelhabr/soccerplotR/issues/new). +#' +#' @examples +#' \donttest{ +#' library(purrr) +#' +#' available_countries <- names(soccerplotR::team_name_mapping) +#' available_countries +#' #> [1] "ENG" "ESP" "FRA" "GER" "ITA" "USA" +#' +#' eng_teams <- soccerplotR::team_name_mapping[["ENG"]] +#' eng_teams[c("Wolves", "Wolverhampton Wanderers", "WBA", "West Brom")] +#' +#' all_teams <- flatten_chr(soccerplotR::team_name_mapping) +#' all_teams[c("Tottenham Hotspur", "Tottenham", "AC Milan")] +#' } +#' +#' @format A list of named character vectors, where each element in the list +#' is an array of teams corresponding to a given country (3-letter ISO code). +#' Each character vector has the following form: +#' \describe{ +#' \item{name attribute}{The "alternate" name.} +#' \item{value attribute}{The "correct" name.} +#' } +"team_name_mapping" diff --git a/R/geom_soccer_logos.R b/R/geom_soccer_logos.R index 5422194..faff395 100644 --- a/R/geom_soccer_logos.R +++ b/R/geom_soccer_logos.R @@ -1,17 +1,17 @@ #' ggplot2 Layer for Visualizing Soccer Team Logos #' #' @description This geom is used to plot soccer team logos instead -#' of points in a ggplot. It requires x, y aesthetics as well as a valid ENG -#' team abbreviation. The latter can be checked with [`valid_team_names()`]. +#' of points in a ggplot. It requires x, y aesthetics as well as a valid +#' team abbreviation. The latter can be checked with [`all_valid_team_names()`](valid_names) +#' or [`valid_team_names()`]. #' #' @inheritParams ggplot2::geom_point -#' @inheritParams valid_team_names #' @section Aesthetics: #' `geom_soccer_logos()` understands the following aesthetics (required aesthetics are in bold): #' \itemize{ #' \item{**x**}{ - The x-coordinate.} #' \item{**y**}{ - The y-coordinate.} -#' \item{**team_name**}{ - The team abbreviation. Should be one of [`valid_team_names()`]. The function tries to clean team names internally by calling [`clean_team_abbrs()`].} +#' \item{**team_name**}{ - The team abbreviation. Should be one of [`team_name_mapping`]. The function tries to clean team names internally by calling [`clean_team_names()`].} #' \item{`alpha = NULL`}{ - The alpha channel, i.e. transparency level, as a numerical value between 0 and 1.} #' \item{`colour = NULL`}{ - The image will be colorized with this colour. Use the special character `'b/w'` to set it to black and white. For more information on valid colour names in ggplot2 see } #' \item{`angle = 0`}{ - The angle of the image as a numerical value between 0° and 360°.} @@ -37,32 +37,32 @@ #' library(soccerplotR) #' library(ggplot2) #' -#' team_name <- soccerplotR::valid_team_names() +#' team_names <- soccerplotR::valid_team_names("ENG") #' #' df <- data.frame( -#' a = rep(1:6, 5), -#' b = sort(rep(1:5, 6), decreasing = TRUE), -#' teams = team_name +#' a = rep(1:6, 8), +#' b = sort(rep(1:8, 6), decreasing = TRUE), +#' team_name = c(team_names, rep(NA, 3)) #' ) #' #' # keep alpha == 1 for all teams including an 'A' -#' matches <- grepl('A', team_name) +#' matches <- grepl('A', df$team_name) #' df$alpha <- ifelse(matches, 1, 0.2) #' # also set a custom fill colour for the non 'A' teams #' df$colour <- ifelse(matches, NA, 'gray') #' #' # scatterplot of all logos #' ggplot(df, aes(x = a, y = b)) + -#' geom_soccer_logos(aes(team_name = teams), width = 0.075) + -#' geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + +#' geom_soccer_logos(aes(team_name = team_name), width = 0.075) + +#' geom_label(aes(label = team_name), nudge_y = -0.35, alpha = 0.5) + #' theme_void() #' #' # apply alpha via an aesthetic from inside the dataset `df` #' # please note that you have to add scale_alpha_identity() to use the alpha #' # values in your dataset! #' ggplot(df, aes(x = a, y = b)) + -#' geom_soccer_logos(aes(team_name = teams, alpha = alpha), width = 0.075) + -#' geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + +#' geom_soccer_logos(aes(team_name = team_name, alpha = alpha), width = 0.075) + +#' geom_label(aes(label = team_name), nudge_y = -0.35, alpha = 0.5) + #' scale_alpha_identity() + #' theme_void() #' @@ -70,16 +70,16 @@ #' # please note that you have to add scale_alpha_identity() as well as #' # scale_color_identity() to use the alpha and colour values in your dataset! #' ggplot(df, aes(x = a, y = b)) + -#' geom_soccer_logos(aes(team_name = teams, alpha = alpha, colour = colour), width = 0.075) + -#' geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + +#' geom_soccer_logos(aes(team_name = team_name, alpha = alpha, colour = colour), width = 0.075) + +#' geom_label(aes(label = team_name), nudge_y = -0.35, alpha = 0.5) + #' scale_alpha_identity() + #' scale_color_identity() + #' theme_void() #' #' # apply alpha as constant for all logos #' ggplot(df, aes(x = a, y = b)) + -#' geom_soccer_logos(aes(team_name = teams), width = 0.075, alpha = 0.6) + -#' geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + +#' geom_soccer_logos(aes(team_name = team_name), width = 0.075, alpha = 0.6) + +#' geom_label(aes(label = team_name), nudge_y = -0.35, alpha = 0.5) + #' theme_void() #' #' } diff --git a/R/scales.R b/R/scales.R index 8a66085..f57b5d4 100644 --- a/R/scales.R +++ b/R/scales.R @@ -1,6 +1,6 @@ #' Scales for Soccer Team Colors #' -#' @description These functions map NBA/WNBA team names to their team colors in +#' @description These functions map soccer team names to their team colors in #' color and fill aesthetics #' @inheritParams ggplot2::scale_fill_manual #' @param type One of `"primary"` or `"secondary"` to decide which color type to use. @@ -16,19 +16,16 @@ #' If `NA` (the default) no transparency will be applied. Can also be a vector of #' alphas. All alpha levels must be in range `[0,1]`. #' @name scale_soccer -#' @seealso The theme elements [element_soccer_logo()] and [element_wsoccer_logo()] to -#' replace axis text labels with logos. #' @aliases NULL #' @examples #' \donttest{ #' library(soccerplotR) #' library(ggplot2) #' -#' COUNTRY <- 'ENG' -#' team_names <- valid_team_names(country = COUNTRY) +#' team_names <- soccerplotR::valid_team_names("ENG") #' #' df <- data.frame( -#' random_value = runif(length(team_abbr), 0, 1), +#' random_value = runif(length(team_names), 0, 1), #' team_name = team_names #' ) #' ggplot(df, aes(x = team_name, y = random_value)) + diff --git a/R/utils.R b/R/utils.R index 99f7d48..91dfa1b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,17 +1,29 @@ -#' Output Valid Soccer Team Abbreviations +#' Output Valid Soccer Team Names #' -#' @description Thenames used in this function are extracted from Fotmob +#' @description Team names used in this function are extracted from Fotmob #' -#' @export -#' @importFrom stats setNames -#' @return A named vector of type `'character'`. Names are ISO 3166 3-letter country codes +#' @param country One of "ENG", "ESP", "FRA", "GER", "ITA", or "USA". (Names are ISO 3166 3-letter country codes.) +#' +#' @return A vector of type `'character'`. +#' @name valid_names #' @examples -#' valid_team_names() -valid_team_names <- function() { +#' all_valid_team_names() +#' valid_team_names("ENG") + +#' @rdname valid_names +#' @export +valid_team_names <- function(country) { + country <- match.arg(country, choices = names(soccerplotR::team_name_mapping)) + sort(unique(soccerplotR::team_name_mapping[[country]])) +} + +#' @rdname valid_names +#' @export +all_valid_team_names <- function() { countries <- names(soccerplotR::team_name_mapping) res <- lapply(countries, function(country) { - sort(unique(soccerplotR::team_name_mapping[[country]])) + valid_team_names(country) }) names(res) <- countries diff --git a/man/geom_soccer_logos.Rd b/man/geom_soccer_logos.Rd index 45e9464..24d8d32 100644 --- a/man/geom_soccer_logos.Rd +++ b/man/geom_soccer_logos.Rd @@ -70,8 +70,9 @@ created with \code{\link[ggplot2:ggplot]{ggplot2::ggplot()}}. } \description{ This geom is used to plot soccer team logos instead -of points in a ggplot. It requires x, y aesthetics as well as a valid ENG -team abbreviation. The latter can be checked with \code{\link[=valid_team_names]{valid_team_names()}}. +of points in a ggplot. It requires x, y aesthetics as well as a valid +team abbreviation. The latter can be checked with \href{valid_names}{\code{all_valid_team_names()}} +or \code{\link[=valid_team_names]{valid_team_names()}}. } \section{Aesthetics}{ @@ -79,7 +80,7 @@ team abbreviation. The latter can be checked with \code{\link[=valid_team_names] \itemize{ \item{\strong{x}}{ - The x-coordinate.} \item{\strong{y}}{ - The y-coordinate.} -\item{\strong{team_name}}{ - The team abbreviation. Should be one of \code{\link[=valid_team_names]{valid_team_names()}}. The function tries to clean team names internally by calling \code{\link[=clean_team_abbrs]{clean_team_abbrs()}}.} +\item{\strong{team_name}}{ - The team abbreviation. Should be one of \code{\link{team_name_mapping}}. The function tries to clean team names internally by calling \code{\link[=clean_team_names]{clean_team_names()}}.} \item{\code{alpha = NULL}}{ - The alpha channel, i.e. transparency level, as a numerical value between 0 and 1.} \item{\code{colour = NULL}}{ - The image will be colorized with this colour. Use the special character \code{'b/w'} to set it to black and white. For more information on valid colour names in ggplot2 see \url{https://ggplot2.tidyverse.org/articles/ggplot2-specs.html?q=colour#colour-and-fill}} \item{\code{angle = 0}}{ - The angle of the image as a numerical value between 0° and 360°.} @@ -101,32 +102,32 @@ A typical size is \code{height = 0.1} (see below examples).} library(soccerplotR) library(ggplot2) -team_name <- soccerplotR::valid_team_names() +team_names <- soccerplotR::valid_team_names("ENG") df <- data.frame( - a = rep(1:6, 5), - b = sort(rep(1:5, 6), decreasing = TRUE), - teams = team_name + a = rep(1:6, 8), + b = sort(rep(1:8, 6), decreasing = TRUE), + team_name = c(team_names, rep(NA, 3)) ) # keep alpha == 1 for all teams including an 'A' -matches <- grepl('A', team_name) +matches <- grepl('A', df$team_name) df$alpha <- ifelse(matches, 1, 0.2) # also set a custom fill colour for the non 'A' teams df$colour <- ifelse(matches, NA, 'gray') # scatterplot of all logos ggplot(df, aes(x = a, y = b)) + - geom_soccer_logos(aes(team_name = teams), width = 0.075) + - geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + + geom_soccer_logos(aes(team_name = team_name), width = 0.075) + + geom_label(aes(label = team_name), nudge_y = -0.35, alpha = 0.5) + theme_void() # apply alpha via an aesthetic from inside the dataset `df` # please note that you have to add scale_alpha_identity() to use the alpha # values in your dataset! ggplot(df, aes(x = a, y = b)) + - geom_soccer_logos(aes(team_name = teams, alpha = alpha), width = 0.075) + - geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + + geom_soccer_logos(aes(team_name = team_name, alpha = alpha), width = 0.075) + + geom_label(aes(label = team_name), nudge_y = -0.35, alpha = 0.5) + scale_alpha_identity() + theme_void() @@ -134,16 +135,16 @@ ggplot(df, aes(x = a, y = b)) + # please note that you have to add scale_alpha_identity() as well as # scale_color_identity() to use the alpha and colour values in your dataset! ggplot(df, aes(x = a, y = b)) + - geom_soccer_logos(aes(team_name = teams, alpha = alpha, colour = colour), width = 0.075) + - geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + + geom_soccer_logos(aes(team_name = team_name, alpha = alpha, colour = colour), width = 0.075) + + geom_label(aes(label = team_name), nudge_y = -0.35, alpha = 0.5) + scale_alpha_identity() + scale_color_identity() + theme_void() # apply alpha as constant for all logos ggplot(df, aes(x = a, y = b)) + - geom_soccer_logos(aes(team_name = teams), width = 0.075, alpha = 0.6) + - geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + + geom_soccer_logos(aes(team_name = team_name), width = 0.075, alpha = 0.6) + + geom_label(aes(label = team_name), nudge_y = -0.35, alpha = 0.5) + theme_void() } diff --git a/man/scale_soccer.Rd b/man/scale_soccer.Rd index 1973e5d..2dfd7d1 100644 --- a/man/scale_soccer.Rd +++ b/man/scale_soccer.Rd @@ -116,7 +116,7 @@ If \code{NA} (the default) no transparency will be applied. Can also be a vector alphas. All alpha levels must be in range \verb{[0,1]}.} } \description{ -These functions map NBA/WNBA team names to their team colors in +These functions map soccer team names to their team colors in color and fill aesthetics } \examples{ @@ -124,11 +124,10 @@ color and fill aesthetics library(soccerplotR) library(ggplot2) -COUNTRY <- 'ENG' -team_names <- valid_team_names(country = COUNTRY) +team_names <- soccerplotR::valid_team_names("ENG") df <- data.frame( - random_value = runif(length(team_abbr), 0, 1), + random_value = runif(length(team_names), 0, 1), team_name = team_names ) ggplot(df, aes(x = team_name, y = random_value)) + @@ -138,7 +137,3 @@ ggplot(df, aes(x = team_name, y = random_value)) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) } } -\seealso{ -The theme elements \code{\link[=element_soccer_logo]{element_soccer_logo()}} and \code{\link[=element_wsoccer_logo]{element_wsoccer_logo()}} to -replace axis text labels with logos. -} diff --git a/man/team_name_mapping.Rd b/man/team_name_mapping.Rd new file mode 100644 index 0000000..9bdcd85 --- /dev/null +++ b/man/team_name_mapping.Rd @@ -0,0 +1,42 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{team_name_mapping} +\alias{team_name_mapping} +\title{Alternate Team Abbreviation Mapping} +\format{ +A list of named character vectors, where each element in the list +is an array of teams corresponding to a given country (3-letter ISO code). +Each character vector has the following form: +\describe{ +\item{name attribute}{The "alternate" name.} +\item{value attribute}{The "correct" name.} +} +} +\usage{ +team_name_mapping +} +\description{ +A list of named character vectors mapping common alternate team abbreviations. +} +\details{ +You can suggest additions to this table by +\href{https://github.com/tonyelhabr/soccerplotR/issues/new}{opening an issue in soccerplotR}. +} +\examples{ +\donttest{ +library(purrr) + +available_countries <- names(soccerplotR::team_name_mapping) +available_countries +#> [1] "ENG" "ESP" "FRA" "GER" "ITA" "USA" + +eng_teams <- soccerplotR::team_name_mapping[["ENG"]] +eng_teams[c("Wolves", "Wolverhampton Wanderers", "WBA", "West Brom")] + +all_teams <- flatten_chr(soccerplotR::team_name_mapping) +all_teams[c("Tottenham Hotspur", "Tottenham", "AC Milan")] +} + +} +\keyword{datasets} diff --git a/man/valid_names.Rd b/man/valid_names.Rd new file mode 100644 index 0000000..7c2c21a --- /dev/null +++ b/man/valid_names.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{valid_names} +\alias{valid_names} +\alias{valid_team_names} +\alias{all_valid_team_names} +\title{Output Valid Soccer Team Names} +\usage{ +valid_team_names(country) + +all_valid_team_names() +} +\arguments{ +\item{country}{One of "ENG", "ESP", "FRA", "GER", "ITA", or "USA". (Names are ISO 3166 3-letter country codes.)} +} +\value{ +A vector of type \code{'character'}. +} +\description{ +Team names used in this function are extracted from Fotmob +} +\examples{ +all_valid_team_names() +valid_team_names("ENG") +} diff --git a/man/valid_team_names.Rd b/man/valid_team_names.Rd deleted file mode 100644 index 0203f62..0000000 --- a/man/valid_team_names.Rd +++ /dev/null @@ -1,17 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{valid_team_names} -\alias{valid_team_names} -\title{Output Valid Soccer Team Abbreviations} -\usage{ -valid_team_names() -} -\value{ -A named vector of type \code{'character'}. Names are ISO 3166 3-letter country codes -} -\description{ -Thenames used in this function are extracted from Fotmob -} -\examples{ -valid_team_names() -}