Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 additions & 23 deletions R/build_grobs.R
Original file line number Diff line number Diff line change
@@ -1,67 +1,86 @@
# INTERNAL HELPER THAT BUILDS THE GROBS FOR
# GEOM LOGOS, WORDMARKS AND HEADSHOTS
build_grobs <- function(i, alpha, colour, data, type = c("teams", "headshots", "wordmarks"), headshot_map = NULL) {
build_grobs <- function(
i,
alpha,
colour,
data,
type = c("teams", "headshots", "wordmarks"),
headshot_map = NULL
) {
make_null <- FALSE
type <- rlang::arg_match(type)
if(type == "teams") {
if (type == "teams") {
team_abbr <- data$team_abbr[i]
image_to_read <- logo_list[[team_abbr]]
if (is.na(team_abbr) | is.null(image_to_read)) make_null <- TRUE
} else if(type == "wordmarks") {
} else if (type == "wordmarks") {
team_abbr <- data$team_abbr[i]
image_to_read <- wordmark_list[[team_abbr]]
if (is.na(team_abbr) | is.null(image_to_read)) make_null <- TRUE
} else {
gsis <- data$player_gsis[i]
image_to_read <- headshot_map$headshot_nfl[headshot_map$gsis_id == gsis]
if(length(image_to_read) == 0 | all(is.na(image_to_read))){
if (length(image_to_read) == 0 | all(is.na(image_to_read))) {
cli::cli_alert_warning(
"No headshot available for gsis ID {.val {data$player_gsis[i]}}. Will insert placeholder."
)
image_to_read <- na_headshot()
}
}
if (isTRUE(make_null)){
if (isTRUE(make_null)) {
cli::cli_alert_warning(
"Can't find team abbreviation {.val {data$team_abbr[i]}}. Will insert empty grob."
)
grid <- grid::nullGrob()
} else if (is.null(alpha)) {
img <- reader_function(image_to_read)
col <- colour[i]
if (!is.null(col) && col %in% "b/w"){
if (!is.null(col) && col %in% "b/w") {
new <- magick::image_quantize(img, colorspace = 'gray')
} else{
} else {
opa <- ifelse(is.na(col) || is.null(col), 0, 100)
col <- ifelse(is.na(col) || is.null(col), "none", col)
new <- magick::image_colorize(img, opa, col)
}
grid <- grid::rasterGrob(new)
} else if (length(alpha) == 1L) {
if (as.numeric(alpha) <= 0 || as.numeric(alpha) >= 1) {
cli::cli_abort("aesthetic {.var alpha} requires a value between {.val 0} and {.val 1}")
cli::cli_abort(
"aesthetic {.var alpha} requires a value between {.val 0} and {.val 1}"
)
}
img <- reader_function(image_to_read)
new <- magick::image_fx(img, expression = paste0(alpha, "*a"), channel = "alpha")
new <- magick::image_fx(
img,
expression = paste0(alpha, "*a"),
channel = "alpha"
)
col <- colour[i]
if (!is.null(col) && col %in% "b/w"){
if (!is.null(col) && col %in% "b/w") {
new <- magick::image_quantize(new, colorspace = 'gray')
} else{
} else {
opa <- ifelse(is.na(col) || is.null(col), 0, 100)
col <- ifelse(is.na(col) || is.null(col), "none", col)
new <- magick::image_colorize(new, opa, col)
}
grid <- grid::rasterGrob(new)
} else {
if (any(as.numeric(alpha) < 0) || any(as.numeric(alpha) > 1)) {
cli::cli_abort("aesthetics {.var alpha} require values between {.val 0} and {.val 1}")
cli::cli_abort(
"aesthetics {.var alpha} require values between {.val 0} and {.val 1}"
)
}
img <- reader_function(image_to_read)
new <- magick::image_fx(img, expression = paste0(alpha[i], "*a"), channel = "alpha")
new <- magick::image_fx(
img,
expression = paste0(alpha[i], "*a"),
channel = "alpha"
)
col <- colour[i]
if (!is.null(col) && col %in% "b/w"){
if (!is.null(col) && col %in% "b/w") {
new <- magick::image_quantize(new, colorspace = 'gray')
} else{
} else {
opa <- ifelse(is.na(col) || is.null(col), 0, 100)
col <- ifelse(is.na(col) || is.null(col), "none", col)
new <- magick::image_colorize(new, opa, col)
Expand All @@ -76,22 +95,19 @@ build_grobs <- function(i, alpha, colour, data, type = c("teams", "headshots", "
height = grid::unit(data$height[i], "npc"),
just = c(data$hjust[i], data$vjust[i]),
angle = data$angle[i],
name = paste("geom_nfl.panel", data$PANEL[i],
"row", i,
sep = "."
)
name = paste("geom_nfl.panel", data$PANEL[i], "row", i, sep = ".")
)

grid$name <- paste("nfl.grob", i, sep = ".")

grid
}

reader_function <- function(img){
if(is.factor(img)) img <- as.character(img)
if(is.raw(img) || tools::file_ext(img) != "svg"){
reader_function <- function(img) {
if (is.factor(img)) img <- as.character(img)
if (is.raw(img) || tools::file_ext(img) != "svg") {
magick::image_read(img)
} else if(tools::file_ext(img) == "svg"){
} else if (tools::file_ext(img) == "svg") {
magick::image_read_svg(img)
}
}
31 changes: 20 additions & 11 deletions R/geom_nfl_headshots.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@
#' # Restore old options
#' options(old)
#' }
geom_nfl_headshots <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE) {

geom_nfl_headshots <- function(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE
) {
ggplot2::layer(
data = data,
mapping = mapping,
Expand All @@ -114,12 +117,18 @@ geom_nfl_headshots <- function(mapping = NULL, data = NULL,
#' @rdname nflplotR-package
#' @export
GeomNFLheads <- ggplot2::ggproto(
"GeomNFLheads", ggplot2::Geom,
"GeomNFLheads",
ggplot2::Geom,
required_aes = c("x", "y", "player_gsis"),
# non_missing_aes = c(""),
default_aes = ggplot2::aes(
alpha = NULL, colour = NULL, angle = 0, hjust = 0.5,
vjust = 0.5, width = 1.0, height = 1.0
alpha = NULL,
colour = NULL,
angle = 0,
hjust = 0.5,
vjust = 0.5,
width = 1.0,
height = 1.0
),
draw_panel = function(data, panel_params, coord, na.rm = FALSE) {
data <- coord$transform(data, panel_params)
Expand All @@ -133,7 +142,7 @@ GeomNFLheads <- ggplot2::ggproto(
data = data,
type = "headshots",
headshot_map = headshots
)
)

class(grobs) <- "gList"

Expand Down
43 changes: 31 additions & 12 deletions R/geom_nfl_logos.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,16 @@
#' coord_cartesian(xlim = c(0.5,2.5), ylim = c(-0.75, 1.75)) +
#' theme_void()
#' }
geom_nfl_logos <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE) {

geom_nfl_logos <- function(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE
) {
ggplot2::layer(
data = data,
mapping = mapping,
Expand All @@ -119,21 +122,37 @@ geom_nfl_logos <- function(mapping = NULL, data = NULL,
#' @rdname nflplotR-package
#' @export
GeomNFLlogo <- ggplot2::ggproto(
"GeomNFLlogo", ggplot2::Geom,
"GeomNFLlogo",
ggplot2::Geom,
required_aes = c("x", "y", "team_abbr"),
# non_missing_aes = c(""),
default_aes = ggplot2::aes(
alpha = NULL, colour = NULL, angle = 0, hjust = 0.5,
vjust = 0.5, width = 1.0, height = 1.0
alpha = NULL,
colour = NULL,
angle = 0,
hjust = 0.5,
vjust = 0.5,
width = 1.0,
height = 1.0
),
draw_panel = function(data, panel_params, coord, na.rm = FALSE) {
data <- coord$transform(data, panel_params)

data$team_abbr <- suppressWarnings(
nflreadr::clean_team_abbrs(as.character(data$team_abbr), keep_non_matches = TRUE)
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")
grobs <- lapply(
seq_along(data$team_abbr),
build_grobs,
alpha = data$alpha,
colour = data$colour,
data = data,
type = "teams"
)

class(grobs) <- "gList"

Expand Down
43 changes: 31 additions & 12 deletions R/geom_nfl_wordmarks.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,16 @@
#' theme_void()
#'
#' }
geom_nfl_wordmarks <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE) {

geom_nfl_wordmarks <- function(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE
) {
ggplot2::layer(
data = data,
mapping = mapping,
Expand All @@ -113,21 +116,37 @@ geom_nfl_wordmarks <- function(mapping = NULL, data = NULL,
#' @rdname nflplotR-package
#' @export
GeomNFLwordmark <- ggplot2::ggproto(
"GeomNFLwordmark", ggplot2::Geom,
"GeomNFLwordmark",
ggplot2::Geom,
required_aes = c("x", "y", "team_abbr"),
# non_missing_aes = c(""),
default_aes = ggplot2::aes(
alpha = NULL, colour = NULL, angle = 0, hjust = 0.5,
vjust = 0.5, width = 1.0, height = 1.0
alpha = NULL,
colour = NULL,
angle = 0,
hjust = 0.5,
vjust = 0.5,
width = 1.0,
height = 1.0
),
draw_panel = function(data, panel_params, coord, na.rm = FALSE) {
data <- coord$transform(data, panel_params)

data$team_abbr <- suppressWarnings(
nflreadr::clean_team_abbrs(as.character(data$team_abbr), keep_non_matches = TRUE)
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 = "wordmarks")
grobs <- lapply(
seq_along(data$team_abbr),
build_grobs,
alpha = data$alpha,
colour = data$colour,
data = data,
type = "wordmarks"
)

class(grobs) <- "gList"

Expand Down
24 changes: 13 additions & 11 deletions R/ggpreview.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@
#' if (rstudioapi::isAvailable()){
#' ggpreview(p, width = 5, asp = 16/9)
#' }
ggpreview <- function(plot = ggplot2::last_plot(),
width = NA,
height = NA,
asp = NULL,
dpi = 300,
device = "png",
units = c("in", "cm", "mm", "px"),
scale = 1,
limitsize = TRUE,
bg = NULL,
...){
ggpreview <- function(
plot = ggplot2::last_plot(),
width = NA,
height = NA,
asp = NULL,
dpi = 300,
device = "png",
units = c("in", "cm", "mm", "px"),
scale = 1,
limitsize = TRUE,
bg = NULL,
...
) {
rlang::check_installed("rstudioapi", reason = "to preview a ggplot file")
file <- tempfile()
if (is.numeric(asp)) height <- width / asp
Expand Down
Loading