Skip to content

Commit

Permalink
Allow nfl logo (#51)
Browse files Browse the repository at this point in the history
* allow non matches in team abbrs
to be able to plot the NFL logo

* catch non matches

* update example with NFL logo

* bump version

* news bullet
  • Loading branch information
mrcaseb authored Dec 12, 2023
1 parent 0525534 commit 9302ad6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: nflplotR
Title: NFL Logo Plots in 'ggplot2'
Version: 1.2.0.9003
Version: 1.2.0.9004
Authors@R:
person("Sebastian", "Carl", , "[email protected]", role = c("aut", "cre"))
Description: A set of functions to visualize National Football League
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* The functions `gt_nfl_logos()` and `gt_nfl_wordmarks()` now keep non team name matches to allow the user to call `gt::sub_missing()`. (#48)
* The functions `gt_nfl_logos()` and `gt_nfl_wordmarks()` now correctly render images in gt row group labels. (#49)
* Deprecated the functions `scale_x_nfl`, `scale_y_nfl`, `scale_x_nfl_headshots`, `scale_y_nfl_headshots`, `theme_x_nfl`, `theme_y_nfl`. These function are slow and require a possibly unstable dependency. Please use the far superior `element_nfl_logo()` and friends instead. (#50)
* The function `geom_nfl_logos()` now plots the NFL logo, if `team_abbr == "NFL"`. (#51)

# nflplotR 1.2.0

Expand Down
4 changes: 2 additions & 2 deletions R/build_grobs.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ build_grobs <- function(i, alpha, colour, data, type = c("teams", "headshots", "
if(type == "teams") {
team_abbr <- data$team_abbr[i]
image_to_read <- logo_list[[team_abbr]]
if (is.na(team_abbr)) make_null <- TRUE
if (is.na(team_abbr) | is.null(image_to_read)) make_null <- TRUE
} else if(type == "wordmarks") {
team_abbr <- data$team_abbr[i]
image_to_read <- wordmark_list[[team_abbr]]
if (is.na(team_abbr)) make_null <- TRUE
if (is.na(team_abbr) | is.null(image_to_read)) make_null <- TRUE
} else if (type == "path"){
image_to_read <- data$path[i]
} else {
Expand Down
15 changes: 7 additions & 8 deletions R/geom_nfl_logos.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' \itemize{
#' \item{**x**}{ - The x-coordinate.}
#' \item{**y**}{ - The y-coordinate.}
#' \item{**team_abbr**}{ - The team abbreviation. Should be one of [`valid_team_names()`]. The function tries to clean team names internally by calling [`nflreadr::clean_team_abbrs()`]}
#' \item{**team_abbr**}{ - The team abbreviation. Should be one of [`valid_team_names()`]. The function tries to clean team names internally by calling [`nflreadr::clean_team_abbrs()`]. Note: `"NFL"`, `"AFC"`, `"NFC"` are valid abbreviations!}
#' \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 Down Expand Up @@ -83,12 +83,11 @@
#' geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) +
#' theme_void()
#'
#' # it's also possible to plot conference logos
#' conf <- data.frame(a = 1:2, b = 0, teams = c("AFC", "NFC"))
#' ggplot(conf, aes(x = a, y = b)) +
#' geom_nfl_logos(aes(team_abbr = teams), width = 0.3) +
#' geom_label(aes(label = teams), nudge_y = -0.4, alpha = 0.5) +
#' coord_cartesian(xlim = c(0.5,2.5), ylim = c(-0.75,.75)) +
#' # it's also possible to plot NFL and conference logos
#' dat <- data.frame(a = c(1.5, 1:2), b = c(1, 0, 0), teams = c("NFL", "AFC", "NFC"))
#' ggplot(dat, aes(x = a, y = b)) +
#' geom_nfl_logos(aes(team_abbr = teams), width = 0.25) +
#' coord_cartesian(xlim = c(0.5,2.5), ylim = c(-0.75, 1.75)) +
#' theme_void()
#' }
geom_nfl_logos <- function(mapping = NULL, data = NULL,
Expand Down Expand Up @@ -126,7 +125,7 @@ GeomNFLlogo <- ggplot2::ggproto(
draw_panel = function(data, panel_params, coord, na.rm = FALSE) {
data <- coord$transform(data, panel_params)

data$team_abbr <- nflreadr::clean_team_abbrs(as.character(data$team_abbr), keep_non_matches = FALSE)
data$team_abbr <- nflreadr::clean_team_abbrs(as.character(data$team_abbr), keep_non_matches = TRUE)

grobs <- lapply(seq_along(data$team_abbr), build_grobs, alpha = data$alpha, colour = data$colour, data = data, type = "teams")

Expand Down
13 changes: 6 additions & 7 deletions man/geom_nfl_logos.Rd

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

0 comments on commit 9302ad6

Please sign in to comment.