This geom is used to plot sports team logos instead
of points in a ggplot. It requires x, y aesthetics as well as a valid
team abbreviation. The latter can be checked with valid_team_names().
Usage
geom_sdv_logos(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
sport = c("nfl", "nba", "wnba", "mlb", "nhl", "cfb", "mbb", "wbb"),
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE
)
GeomSDVlogoArguments
- mapping
Set of aesthetic mappings created by
aes(). If specified andinherit.aes = TRUE(the default), it is combined with the default mapping at the top level of the plot. You must supplymappingif 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 toggplot().A
data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. Seefortify()for which variables will be created.A
functionwill be called with a single argument, the plot data. The return value must be adata.frame, and will be used as the layer data. Afunctioncan be created from aformula(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, thestatargument can be used to override the default coupling between geoms and stats. Thestatargument accepts the following:A
Statggproto subclass, for exampleStatCount.A string naming the stat. To give the stat as a string, strip the function name of the
stat_prefix. For example, to usestat_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
positionargument 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 useposition_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
layer()'sparamsargument. These arguments broadly fall into one of 4 categories below. Notably, further arguments to thepositionargument, or aesthetics that are required can not be passed through.... Unknown arguments that are not part of the 4 categories below are ignored.Static aesthetics that are not mapped to a scale, but are at a fixed value and apply to the layer as a whole. For example,
colour = "red"orlinewidth = 3. The geom's documentation has an Aesthetics section that lists the available options. The 'required' aesthetics cannot be passed on to theparams. Please note that while passing unmapped aesthetics as vectors is technically possible, the order and required length is not guaranteed to be parallel to the input data.When constructing a layer using a
stat_*()function, the...argument can be used to pass on parameters to thegeompart of the layer. An example of this isstat_density(geom = "area", outline.type = "both"). The geom's documentation lists which parameters it can accept.Inversely, when constructing a layer using a
geom_*()function, the...argument can be used to pass on parameters to thestatpart of the layer. An example of this isgeom_area(stat = "density", adjust = 0.5). The stat's documentation lists which parameters it can accept.The
key_glyphargument oflayer()may also be passed on through.... This can be one of the functions described as key glyphs, to change the display of the layer in the legend.
- sport
Character string identifying the sport. One of:
"nfl","nba","wnba","mlb","nhl","cfb","mbb","wbb".- na.rm
If
FALSE, the default, missing values are removed with a warning. IfTRUE, 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.FALSEnever includes, andTRUEalways 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, useTRUE. IfNA, 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_sdv_logos() understands the following aesthetics (required aesthetics are in bold):
- x
- The x-coordinate.
- y
- The y-coordinate.
- team
- The team abbreviation. Should be one of
valid_team_names(). The function tries to clean team names internally.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.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.
vjust = 0.5- The vertical adjustment relative to the given y coordinate.
width = 1.0- The desired width of the image in
npc(Normalised Parent Coordinates). A typical size iswidth = 0.075.height = 1.0- The desired height of the image in
npc. A typical size isheight = 0.1.season = NA- The season whose logo to draw: the ending year for the NHL (
1990for 1989-90), the year elsewhere. Unmapped orNA, the team's logo today. See the Historical logos section.
Historical logos
Given a season, a team is drawn with the mark it wore that season where
sdvplotR has one, and with today's logo otherwise. Coverage:
NHL: every club identity since 1917-18, primary and dark marks from the NHL's own logo catalog; a club's current era draws today's logo. Teams are keyed by the NHL triCode of that identity (
"QUE","HFD","ATL","MNS","TBL"); current clubs also answer to sdvplotR's abbreviation and full name ("TB","Tampa Bay Lightning").NFL and WNBA: relocated and defunct identities only, one mark each: NFL
"STL"(1995-2015) and"SD"(1961-2016); WNBA"HOU","SAC","CHA","DET","TUL","SAS"and"SA".No other league has per-season logos yet.
A key only finds its own identity's marks: "COL" in 1990 draws today's
Avalanche logo (the Nordiques are "QUE"), and "STL" in 2020 today's
Rams. The images are copies kept in the SportsDataverse asset archive, so
they don't change when a league reuses a file name. The NHL marks are SVG
files, which 'ggpath' reads with the 'rsvg' package. season takes single
years: the ending year for the NHL (2005 for 2004-05).
Examples
# \donttest{
library(sdvplotR)
library(ggplot2)
team_abbr <- valid_team_names("nfl")[1:16]
df <- data.frame(
a = rep(1:4, 4),
b = sort(rep(1:4, 4), decreasing = TRUE),
teams = team_abbr
)
ggplot(df, aes(x = a, y = b)) +
geom_sdv_logos(aes(team = teams), sport = "nfl", width = 0.075) +
geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) +
theme_void()
# the logo each team wore that season (NHL seasons are ending years;
# the NHL's marks are SVG files, read with the 'rsvg' package)
if (requireNamespace("rsvg", quietly = TRUE)) {
old <- data.frame(
x = 1:4, y = 1,
team = c("QUE", "HFD", "ATL", "WPG"),
season = c(1990, 1995, 2005, 2015)
)
ggplot(old, aes(x = x, y = y)) +
geom_sdv_logos(aes(team = team, season = season), sport = "nhl", width = 0.2) +
theme_void()
}
# }
