Package 'futebolplotR'

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

Help Index


Standardize Brazilian Soccer Team Abbreviations

Description

This function standardizes Brazilian Soccer team abbreviations to the abbreviations used in futebolplotR (see futebol_team_abbr_mapping).

Usage

clean_team_abbrs(abbr, keep_non_matches = TRUE)

Arguments

abbr

a character vector of abbreviations

keep_non_matches

If TRUE (the default) an element of abbr that can't be matched to any of the internal mapping vectors will be kept as is. Otherwise it will be replaced with NA.

Value

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).


Theme Elements for Image Grobs

Description

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.

Usage

element_futebol_badge(
  alpha = 1L,
  colour = NA_character_,
  hjust = 0.5,
  vjust = 0.5,
  color = NULL,
  angle = 0,
  size = grid::unit(0.5, "cm")
)

Arguments

alpha

The alpha channel, i.e. transparency level, as a numerical value between 0 and 1. 1L skips alpha channel modification for more speed.

colour, color

The image will be colorized with this colour. Use the special character "b/w" to set it to black and white.

hjust

A numeric vector specifying horizontal justification. If specified, overrides the just setting.

vjust

A numeric vector specifying vertical justification. If specified, overrides the just setting.

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, cm will be applied as unit. Stored in the image_size property so the inherited text size property retains the numeric type required by ggplot2::element_text().

Details

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.

Value

An S7 object of class element_futebol_badge.

See Also

geom_futebol_badges(), and ggpath::element_path() for more information on valid team abbreviations, and other parameters.

Examples

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))

Brazilian Soccer Team Abbreviation Mapping

Description

A named character vector mapping alternative team abbreviations (names) to the standard abbreviations used in futebolplotR (values). It is used by clean_team_abbrs().

Usage

futebol_team_abbr_mapping

Format

A named character vector.

Source

data-raw/teams.csv and data-raw/build_internal_data.R

Examples

futebol_team_abbr_mapping

ggplot2 Layer for Visualizing Brazilian Soccer Team badges

Description

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().

Usage

geom_futebol_badges(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  na.rm = FALSE,
  show.legend = FALSE,
  inherit.aes = TRUE
)

Arguments

mapping

Set of aesthetic mappings created by aes(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)).

stat

The statistical transformation to use on the data for this layer. When using a ⁠geom_*()⁠ function to construct a layer, the stat argument can be used to override the default coupling between geoms and stats. The stat argument accepts the following:

  • A Stat ggproto subclass, for example StatCount.

  • A string naming the stat. To give the stat as a string, strip the function name of the stat_ prefix. For example, to use stat_count(), give the stat as "count".

  • For more information and other ways to specify the stat, see the layer stat documentation.

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 position argument accepts the following:

  • The result of calling a position function, such as position_jitter(). This method allows for passing extra arguments to the position.

  • A string naming the position adjustment. To give the position as a string, strip the function name of the position_ prefix. For example, to use position_jitter(), give the position as "jitter".

  • For more information and other ways to specify the position, see the layer position documentation.

...

Other arguments passed on to ggplot2::layer(). These are often aesthetics, used to set an aesthetic to a fixed value. See the below section "Aesthetics" for a full list of possible arguments.

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display. To include legend keys for all levels, even when no data exists, use TRUE. If NA, all levels are shown in legend, but unobserved levels are omitted.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. annotation_borders().

Value

A ggplot2 layer (ggplot2::layer()) that can be added to a plot created with ggplot2::ggplot().

Aesthetics

geom_futebol_badges() understands the following aesthetics (required aesthetics are in bold):

x

The x-coordinate.

y

The y-coordinate.

team_abbr

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 = NULL

The alpha channel, i.e. transparency level, as a numerical value between 0 and 1.

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

angle = 0

The angle of the image as a numerical value between 0 and 360 degrees.

hjust = 0.5

The horizontal adjustment relative to the given x coordinate. Must be a numerical value between 0 and 1.

vjust = 0.5

The vertical adjustment relative to the given y coordinate. Must be a numerical value between 0 and 1.

width = 1.0

The 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.0

The 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).

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()

Scales for Brazilian Soccer Team Colors

Description

These functions map soccer team names to their team colors in color and fill aesthetics

Usage

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
)

Arguments

type

One of "primary" or "secondary" to decide which color type to use.

values

If NULL (the default) use the internal team color vectors. Otherwise a set of aesthetic values to map data values to. The values will be matched in order (usually alphabetical) with the limits of the scale, or with breaks if provided. If this is a named vector, then the values will be matched based on the names instead. Data values that don't match will be given na.value.

...

Arguments passed on to discrete_scale

limits

One of:

  • NULL to use the default scale values

  • A character vector that defines possible values of the scale and their order

  • A function that accepts the existing (automatic) values and returns new ones. Also accepts rlang lambda function notation.

drop

Should unused factor levels be omitted from the scale? The default, TRUE, uses the levels that appear in the data; FALSE includes the levels in the factor. Please note that to display every level in a legend, the layer should use show.legend = TRUE.

na.translate

Unlike continuous scales, discrete scales can easily show missing values, and do so by default. If you want to remove missing values from a discrete scale, specify na.translate = FALSE.

name

The name of the scale. Used as the axis or legend title. If waiver(), the default, the name of the scale is taken from the first mapping used for that aesthetic. If NULL, the legend title will be omitted.

minor_breaks

One of:

  • NULL for no minor breaks

  • waiver() for the default breaks (none for discrete, one minor break between each major break for continuous)

  • A numeric vector of positions

  • A function that given the limits returns a vector of minor breaks. Also accepts rlang lambda function notation. When the function has two arguments, it will be given the limits and major break positions.

labels

One of the options below. Please note that when labels is a vector, it is highly recommended to also set the breaks argument as a vector to protect against unintended mismatches.

  • NULL for no labels

  • waiver() for the default labels computed by the transformation object

  • A character vector giving labels (must be same length as breaks)

  • An expression vector (must be the same length as breaks). See ?plotmath for details.

  • A function that takes the breaks as input and returns labels as output. Also accepts rlang lambda function notation.

guide

A function used to create a guide or its name. See guides() for more information.

call

The call used to construct the scale for reporting messages.

super

The super class to use for the constructed scale

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 colour and fill aesthetics at the same time, via aesthetics = c("colour", "fill").

breaks

One of:

  • NULL for no breaks

  • waiver() for the default breaks (the scale limits)

  • A character vector of breaks

  • A function that takes the limits as input and returns breaks as output

na.value

The aesthetic value to use for missing (NA) values

guide

A function used to create a guide or its name. If NULL (the default) no guide will be plotted for this scale. See ggplot2::guides() for more information.

alpha

Factor to modify color transparency via a call to scales::alpha(). 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]⁠.

See Also

The theme elements element_futebol_badge() to replace axis text labels with badges.

Examples

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))

Brazilian Soccer Team Names and Colors

Description

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().

Usage

teams_colors_badges

Format

A data frame with one row per team and 5 variables:

team

Team name.

abbr

Team abbreviation. See valid_team_names().

state

Brazilian state of the team.

color1

Primary team color.

color2

Secondary team color.

Source

data-raw/teams.csv and data-raw/build_internal_data.R

Examples

head(teams_colors_badges)

Output Valid Brazilian Soccer Team Abbreviations

Description

Output Valid Brazilian Soccer Team Abbreviations

Usage

valid_team_names()

Value

A vector of type "character".

Examples

# List valid Brazilian soccer team abbreviations
valid_team_names()