| Title: | Brazilian Soccer Badges Plots in 'ggplot2' |
|---|---|
| Description: | A set of functions to visualize Brazilian Soccer analysis in 'ggplot2'. |
| Authors: | Bruno Mioto [cre, aut], Gabriela Junqueira [aut] |
| Maintainer: | Bruno Mioto <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-10-20 15:23:07 UTC |
| Source: | https://github.com/brunomioto/futebolplotR |
This function standardizes Brazilian Soccer team abbreviations to the
abbreviations used in futebolplotR (see futebol_team_abbr_mapping).
clean_team_abbrs(abbr, keep_non_matches = TRUE)clean_team_abbrs(abbr, keep_non_matches = TRUE)
abbr |
a character vector of abbreviations |
keep_non_matches |
If |
A character vector with the length of abbr and cleaned team abbreviations
if they are included in futebol_team_abbr_mapping.
Non matches may be replaced with NA (depending on the value of keep_non_matches).
In conjunction with the ggplot2::theme system, the following element_
functions enable images in non-data components of the plot, e.g. axis text.
element_futebol_badge(): draws Brazilian Soccer team badges instead of their abbreviations.
element_futebol_badge( alpha = 1L, colour = NA_character_, hjust = 0.5, vjust = 0.5, color = NULL, angle = 0, size = grid::unit(0.5, "cm") )element_futebol_badge( alpha = 1L, colour = NA_character_, hjust = 0.5, vjust = 0.5, color = NULL, angle = 0, size = grid::unit(0.5, "cm") )
alpha |
The alpha channel, i.e. transparency level, as a numerical value
between 0 and 1. |
colour, color
|
The image will be colorized with this colour. Use the
special character |
hjust |
A numeric vector specifying horizontal justification.
If specified, overrides the |
vjust |
A numeric vector specifying vertical justification.
If specified, overrides the |
angle |
The angle of the element as a numerical value between 0° and 360°. |
size |
The output grob size as a grid::unit. If given a numeric,
|
The elements translate Brazilian Soccer team abbreviations into badge images. Unknown abbreviations are drawn as a generic badge.
The complete themes of ggplot2 (e.g. ggplot2::theme_minimal()) set
axis.text.x.bottom and axis.text.y.left directly. Elements set in
axis.text.x or axis.text.y are then overwritten and no badge will be
drawn. Please use the specific elements, i.e. axis.text.x.bottom,
axis.text.x.top, axis.text.y.left or axis.text.y.right.
An S7 object of class element_futebol_badge.
geom_futebol_badges(), and ggpath::element_path()
for more information on valid team abbreviations, and other parameters.
library(futebolplotR) library(ggplot2) team_abbr <- valid_team_names() df <- data.frame( random_value = runif(length(team_abbr), 0, 1), team = team_abbr ) # use badges for x-axis ggplot(df, aes(x = team, y = random_value)) + geom_col(aes(color = team, fill = team), width = 0.5) + scale_color_futebol(type = "secondary") + scale_fill_futebol(alpha = 0.4) + theme_minimal() + theme(axis.text.x.bottom = element_futebol_badge()) # use badges for y-axis ggplot(df, aes(y = team, x = random_value)) + geom_col(aes(color = team, fill = team), width = 0.5) + scale_color_futebol(type = "secondary") + scale_fill_futebol(alpha = 0.4) + theme_minimal() + theme(axis.text.y.left = element_futebol_badge()) # black and white badges with transparency ggplot(df, aes(x = team, y = random_value)) + geom_col(width = 0.5) + theme_minimal() + theme(axis.text.x.bottom = element_futebol_badge(colour = "b/w", alpha = 0.6))library(futebolplotR) library(ggplot2) team_abbr <- valid_team_names() df <- data.frame( random_value = runif(length(team_abbr), 0, 1), team = team_abbr ) # use badges for x-axis ggplot(df, aes(x = team, y = random_value)) + geom_col(aes(color = team, fill = team), width = 0.5) + scale_color_futebol(type = "secondary") + scale_fill_futebol(alpha = 0.4) + theme_minimal() + theme(axis.text.x.bottom = element_futebol_badge()) # use badges for y-axis ggplot(df, aes(y = team, x = random_value)) + geom_col(aes(color = team, fill = team), width = 0.5) + scale_color_futebol(type = "secondary") + scale_fill_futebol(alpha = 0.4) + theme_minimal() + theme(axis.text.y.left = element_futebol_badge()) # black and white badges with transparency ggplot(df, aes(x = team, y = random_value)) + geom_col(width = 0.5) + theme_minimal() + theme(axis.text.x.bottom = element_futebol_badge(colour = "b/w", alpha = 0.6))
A named character vector mapping alternative team abbreviations (names)
to the standard abbreviations used in futebolplotR (values). It is used by
clean_team_abbrs().
futebol_team_abbr_mappingfutebol_team_abbr_mapping
A named character vector.
data-raw/teams.csv and data-raw/build_internal_data.R
futebol_team_abbr_mappingfutebol_team_abbr_mapping
This geom is used to plot Brazilian soccer team badges instead
of points in a ggplot. It requires x, y aesthetics as well as a valid Brazilian soccer
team abbreviation. The latter can be checked with valid_team_names().
geom_futebol_badges( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., na.rm = FALSE, show.legend = FALSE, inherit.aes = TRUE )geom_futebol_badges( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., na.rm = FALSE, show.legend = FALSE, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
... |
Other arguments passed on to |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
A ggplot2 layer (ggplot2::layer()) that can be added to a plot
created with ggplot2::ggplot().
geom_futebol_badges() understands the following aesthetics (required aesthetics are in bold):
The x-coordinate.
The y-coordinate.
The team abbreviation. Should be one of valid_team_names(). The function tries to clean team names internally by calling clean_team_abbrs(). Unknown abbreviations are drawn as a generic badge.
alpha = NULLThe alpha channel, i.e. transparency level, as a numerical value between 0 and 1.
colour = NULLThe 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
angle = 0The angle of the image as a numerical value between 0 and 360 degrees.
hjust = 0.5The horizontal adjustment relative to the given x coordinate. Must be a numerical value between 0 and 1.
vjust = 0.5The vertical adjustment relative to the given y coordinate. Must be a numerical value between 0 and 1.
width = 1.0The desired width of the image in npc (Normalised Parent Coordinates).
The default value is set to 1.0 which is big but it is necessary
because all used values are computed relative to the default.
A typical size is width = 0.075 (see below examples).
height = 1.0The desired height of the image in npc (Normalised Parent Coordinates).
The default value is set to 1.0 which is big but it is necessary
because all used values are computed relative to the default.
A typical size is height = 0.1 (see below examples).
library(futebolplotR) library(ggplot2) team_abbr <- futebolplotR::valid_team_names() df <- data.frame( a = rep(1:5, 2), b = sort(rep(1:2, 5), decreasing = TRUE), teams = team_abbr[1:10] ) # keep alpha == 1 for all teams including an "A" matches <- grepl("A", team_abbr[1:10]) 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 badges ggplot(df, aes(x = a, y = b)) + geom_futebol_badges(aes(team_abbr = teams), width = 0.075) + geom_label(aes(label = teams), 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_futebol_badges(aes(team_abbr = teams, alpha = alpha), width = 0.075) + geom_label(aes(label = teams), 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_futebol_badges(aes(team_abbr = teams, alpha = alpha, colour = colour), width = 0.075) + geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + scale_alpha_identity() + scale_color_identity() + theme_void() # apply alpha as constant for all badges ggplot(df, aes(x = a, y = b)) + geom_futebol_badges(aes(team_abbr = teams), width = 0.075, alpha = 0.6) + geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + theme_void()library(futebolplotR) library(ggplot2) team_abbr <- futebolplotR::valid_team_names() df <- data.frame( a = rep(1:5, 2), b = sort(rep(1:2, 5), decreasing = TRUE), teams = team_abbr[1:10] ) # keep alpha == 1 for all teams including an "A" matches <- grepl("A", team_abbr[1:10]) 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 badges ggplot(df, aes(x = a, y = b)) + geom_futebol_badges(aes(team_abbr = teams), width = 0.075) + geom_label(aes(label = teams), 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_futebol_badges(aes(team_abbr = teams, alpha = alpha), width = 0.075) + geom_label(aes(label = teams), 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_futebol_badges(aes(team_abbr = teams, alpha = alpha, colour = colour), width = 0.075) + geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + scale_alpha_identity() + scale_color_identity() + theme_void() # apply alpha as constant for all badges ggplot(df, aes(x = a, y = b)) + geom_futebol_badges(aes(team_abbr = teams), width = 0.075, alpha = 0.6) + geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) + theme_void()
These functions map soccer team names to their team colors in color and fill aesthetics
scale_color_futebol( type = c("primary", "secondary"), values = NULL, ..., aesthetics = "colour", breaks = ggplot2::waiver(), na.value = "grey50", guide = NULL, alpha = NA ) scale_colour_futebol( type = c("primary", "secondary"), values = NULL, ..., aesthetics = "colour", breaks = ggplot2::waiver(), na.value = "grey50", guide = NULL, alpha = NA ) scale_fill_futebol( type = c("primary", "secondary"), values = NULL, ..., aesthetics = "fill", breaks = ggplot2::waiver(), na.value = "grey50", guide = NULL, alpha = NA )scale_color_futebol( type = c("primary", "secondary"), values = NULL, ..., aesthetics = "colour", breaks = ggplot2::waiver(), na.value = "grey50", guide = NULL, alpha = NA ) scale_colour_futebol( type = c("primary", "secondary"), values = NULL, ..., aesthetics = "colour", breaks = ggplot2::waiver(), na.value = "grey50", guide = NULL, alpha = NA ) scale_fill_futebol( type = c("primary", "secondary"), values = NULL, ..., aesthetics = "fill", breaks = ggplot2::waiver(), na.value = "grey50", guide = NULL, alpha = NA )
type |
One of |
values |
If |
... |
Arguments passed on to
|
aesthetics |
Character string or vector of character strings listing the
name(s) of the aesthetic(s) that this scale works with. This can be useful, for
example, to apply colour settings to the |
breaks |
One of:
|
na.value |
The aesthetic value to use for missing ( |
guide |
A function used to create a guide or its name. If |
alpha |
Factor to modify color transparency via a call to |
The theme elements element_futebol_badge() to
replace axis text labels with badges.
library(futebolplotR) library(ggplot2) team_abbr <- valid_team_names() df <- data.frame( random_value = runif(length(team_abbr), 0, 1), teams = team_abbr ) ggplot(df, aes(x = teams, y = random_value)) + geom_col(aes(color = teams, fill = teams), width = 0.5) + scale_color_futebol(type = "secondary") + scale_fill_futebol(alpha = 0.4) + theme_minimal() + theme(axis.text.x.bottom = element_text(angle = 45, hjust = 1))library(futebolplotR) library(ggplot2) team_abbr <- valid_team_names() df <- data.frame( random_value = runif(length(team_abbr), 0, 1), teams = team_abbr ) ggplot(df, aes(x = teams, y = random_value)) + geom_col(aes(color = teams, fill = teams), width = 0.5) + scale_color_futebol(type = "secondary") + scale_fill_futebol(alpha = 0.4) + theme_minimal() + theme(axis.text.x.bottom = element_text(angle = 45, hjust = 1))
A data frame of all teams with a badge in futebolplotR and their colors.
The colors are used in scale_color_futebol() and scale_fill_futebol().
teams_colors_badgesteams_colors_badges
A data frame with one row per team and 5 variables:
Team name.
Team abbreviation. See valid_team_names().
Brazilian state of the team.
Primary team color.
Secondary team color.
data-raw/teams.csv and data-raw/build_internal_data.R
head(teams_colors_badges)head(teams_colors_badges)
Output Valid Brazilian Soccer Team Abbreviations
valid_team_names()valid_team_names()
A vector of type "character".
# List valid Brazilian soccer team abbreviations valid_team_names()# List valid Brazilian soccer team abbreviations valid_team_names()