Skip to contents

Places team marks on a plotly::plot_ly() chart at data coordinates, as layout images. The R counterpart of sdvplot's plotly adapter. Works on native plotly charts; plotly::ggplotly() drops layout images, so draw a ggplot with geom_sdv_logos() or geom_sdv_logos_interactive() instead.

Usage

add_sdv_logos(
  p,
  x,
  y,
  team,
  league,
  size = 0.08,
  season = NULL,
  variant = "default",
  id_system = "auto",
  strict = FALSE,
  xref = "x",
  yref = "y"
)

add_sdv_wordmarks(
  p,
  x,
  y,
  team,
  league,
  size = 0.08,
  season = NULL,
  variant = "default",
  id_system = "auto",
  strict = FALSE,
  xref = "x",
  yref = "y"
)

add_sdv_headshots(
  p,
  x,
  y,
  player_id,
  league,
  size = 0.08,
  id_system = "espn",
  xref = "x",
  yref = "y"
)

Arguments

p

A plotly object.

x, y

Positions: numbers, or category names on a categorical axis.

team

Team identifiers, one per position.

league

A league key from sdv_leagues(). Required: one abbreviation means different teams in different leagues.

size

Each image is fitted by height to size times the y-axis span (see Details), so 0.08 is 8% of the axis. The width follows the image's aspect ratio.

season

NULL for today's mark, or a season (or one per team): the mark the team wore that season where the archive dates its marks.

variant

"default", "dark", or a named variant from sdv_marks() ("scoreboard", "grayscale", "cap_on_light", ...). A team without it gets its closest mark of the same polarity; a name no mark in the archive has is an error listing the league's variants.

id_system

For headshots, as in sdv_headshot_url().

strict

If TRUE, a value that does not resolve is an error; otherwise it becomes NA with one warning per call.

xref, yref

The axes the positions refer to ("x2" for a subplot's).

player_id

Player ids, one per position.

Value

The plotly object (an htmlwidget) with the images added.

Details

Images are placed in data units and fitted by height: each is sized to size times the y-axis span, as sdvplot's plotly adapter sizes them. The span is the axis range when one is set, and otherwise everything the chart's traces on that axis cover plus the new marks (the number of categories on a categorical axis), so marks for a subset of the plotted points are sized against the whole axis. Images already on the chart, including ones set with plotly::layout(), are kept. Date axes are not supported yet.

Examples

# \donttest{
if (requireNamespace("plotly", quietly = TRUE)) {
  d <- data.frame(
    team = c("KC", "BUF", "BAL"), off = c(0.12, 0.15, 0.18), def = c(-0.05, 0.02, -0.08)
  )
  plotly::plot_ly(d, x = ~off, y = ~def, type = "scatter", mode = "markers", text = ~team) |>
    add_sdv_logos(d$off, d$def, d$team, "nfl")
}
# }