Skip to content

Commit

Permalink
devtools check all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyelhabr committed Jan 21, 2024
1 parent 056f7a1 commit 542602a
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 73 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -11,4 +12,3 @@ import(ggpath)
import(grid)
importFrom(cli,cli_warn)
importFrom(ggplot2,element_grob)
importFrom(stats,setNames)
30 changes: 30 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 17 additions & 17 deletions R/geom_soccer_logos.R
Original file line number Diff line number Diff line change
@@ -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 <https://ggplot2.tidyverse.org/articles/ggplot2-specs.html?q=colour#colour-and-fill>}
#' \item{`angle = 0`}{ - The angle of the image as a numerical value between 0° and 360°.}
Expand All @@ -37,49 +37,49 @@
#' 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()
#'
#' # apply alpha and colour via an aesthetic from inside the dataset `df`
#' # 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()
#'
#' }
Expand Down
9 changes: 3 additions & 6 deletions R/scales.R
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)) +
Expand Down
28 changes: 20 additions & 8 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 17 additions & 16 deletions man/geom_soccer_logos.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions man/scale_soccer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions man/team_name_mapping.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/valid_names.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 0 additions & 17 deletions man/valid_team_names.Rd

This file was deleted.

0 comments on commit 542602a

Please sign in to comment.