Giter VIP home page Giter VIP logo

teamcolors's Introduction

teamcolors

R-CMD-check CRAN status

An R package providing color palettes for pro and amateur sports teams. The palettes are provided by Jim Neilsen’s Team Colors website and offered with only minimal alterations. NCAA colors come from teamcolorcodes.com, via the ncaahoopR package. Other sports include the Women’s National Basketball Association, National Women’s Soccer League, and the Canadian Football League.

Install

To install the CRAN version, use:

install.packages(teamcolors)

To install the development version from GitHub, use:

devtools::install_github("beanumber/teamcolors")

Load

library(teamcolors)
head(teamcolors)
## # A tibble: 6 × 11
##   name     league primary secon…¹ terti…² quate…³ divis…⁴ locat…⁵ mascot sport…⁶
##   <chr>    <chr>  <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>  <chr>  
## 1 A&M-Cor… ncaa   #0067C5 #007F3E <NA>    <NA>    Southl… A&M-Co… Chris… A&M-Co…
## 2 AFC Bou… epl    #e62333 #000000 <NA>    <NA>    <NA>    AFC     Bourn… AFC Bo…
## 3 Abilene… ncaa   #461D7C #FFFFFF <NA>    <NA>    WAC     Abilene Chris… Abilen…
## 4 Air For… ncaa   #003087 #8A8D8F #B1B3B3 #FFC72C MWC     Air Fo… <NA>   Air Fo…
## 5 Akron    ncaa   #041E42 #A89968 <NA>    <NA>    MAC     Akron   <NA>   Akron  
## 6 Alabama  ncaa   #9E1B32 #828A8F #FFFFFF <NA>    SEC     Alabama <NA>   Alabama
## # … with 1 more variable: logo <chr>, and abbreviated variable names
## #   ¹​secondary, ²​tertiary, ³​quaternary, ⁴​division, ⁵​location, ⁶​sportslogos_name

Show palettes

Sometimes you need to work with a named vector of colors. Other times you can use the built-in scale_color_teams() and scale_color_fill() functions.

league_pal("nba")
##          Atlanta Hawks         Boston Celtics          Brooklyn Nets 
##              "#e13a3e"              "#008348"              "#061922" 
##      Charlotte Hornets          Chicago Bulls    Cleveland Cavaliers 
##              "#1d1160"              "#ce1141"              "#860038" 
##       Dallas Mavericks         Denver Nuggets        Detroit Pistons 
##              "#007dc5"              "#4d90cd"              "#ed174c" 
##  Golden State Warriors        Houston Rockets         Indiana Pacers 
##              "#fdb927"              "#ce1141"              "#ffc633" 
##   Los Angeles Clippers     Los Angeles Lakers      Memphis Grizzlies 
##              "#ed174c"              "#fdb927"              "#0f586c" 
##             Miami Heat        Milwaukee Bucks Minnesota Timberwolves 
##              "#98002e"              "#00471b"              "#005083" 
##   New Orleans Pelicans        New York Knicks  Oklahoma City Thunder 
##              "#002b5c"              "#006bb6"              "#007dc3" 
##          Orlando Magic     Philadelphia 76ers           Phoenix Suns 
##              "#007dc5"              "#ed174c"              "#e56020" 
## Portland Trail Blazers       Sacramento Kings      San Antonio Spurs 
##              "#e03a3e"              "#724c9f"              "#bac3c9" 
##        Toronto Raptors              Utah Jazz     Washington Wizards 
##              "#ce1141"              "#002b5c"              "#002b5c"

Plot

In baseball, Pythagorean expectation relates expected winning percentage to runs allowed and runs scored. How well does it work?

library(Lahman)
library(tidyverse)
pythag <- Teams %>%
  filter(yearID == 2016) %>%
  select(name, teamID, yearID, W, L, R, RA) %>%
  mutate(
    wpct = W / (W + L), exp_wpct = 1 / (1 + (RA / R)^2),
    # note name discrepancy!
    name = ifelse(name == "Los Angeles Angels of Anaheim", "Los Angeles Angels", name)
  )

ggplot2

ggplot(pythag, aes(x = wpct, y = exp_wpct, color = name, fill = name)) +
  geom_abline(slope = 1, intercept = 0, linetype = 3) +
  geom_point(shape = 21, size = 3) +
  scale_fill_teams(guide = FALSE) +
  scale_color_teams(2, guide = FALSE) +
  ggrepel::geom_text_repel(aes(label = teamID)) +
  scale_x_continuous("Winning Percentage", limits = c(0.3, 0.7)) +
  scale_y_continuous("Expected Winning Percentage", limits = c(0.3, 0.7)) +
  theme_light() +
  labs(
    title = "Real and Pythagorean winning % by team",
    subtitle = paste(first(pull(pythag, yearID)), "MLB Season", sep = " "),
    caption = "Source: Lahman baseball database. Using teamcolors R pkg"
  ) +
  coord_equal()
## Warning: The `guide` argument in `scale_*()` cannot be `FALSE`. This was deprecated in
## ggplot2 3.3.4.
## ℹ Please use "none" instead.

## Warning: ggrepel: 4 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

Base R

pythag <- pythag %>%
  left_join(teamcolors, by = "name")
with(pythag, plot(wpct, exp_wpct, bg = primary, col = secondary, pch = 21, cex = 3))

Key

You can see the color palettes using existing functionality from the scales package, but it won’t show the names of the teams.

scales::show_col(league_pal("mlb"), borders = league_pal("mlb", 2))

So, instead, use show_team_col(). Note that this only shows color palettes for non-NCAA teams.

show_team_col()
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the teamcolors package.
##   Please report the issue at <�]8;;https://github.com/beanumber/teamcolors/issues�https://github.com/beanumber/teamcolors/issues�]8;;�>.

To view color palettes for college teams, use the show_ncaa_col() function.

show_ncaa_col()

Logos

Links to team logos are provided by (http://www.sportslogos.net/).

teamcolors %>%
  filter(grepl("New Y", name)) %>%
  pull(logo) %>%
  knitr::include_graphics()

Note that we don’t have any coverage for the EPL.

teamcolors %>%
  group_by(league) %>%
  summarize(
    num_teams = n(),
    num_logos = sum(!is.na(logo))
  )
## # A tibble: 10 × 3
##    league num_teams num_logos
##    <chr>      <int>     <int>
##  1 cfl            9         8
##  2 epl           20         0
##  3 mlb           30        30
##  4 mls           22        21
##  5 nba           30        30
##  6 ncaa         363       235
##  7 nfl           32        31
##  8 nhl           31        31
##  9 nwsl           9         7
## 10 wnba          12        12

References

For more examples see:

  • Lopez, M.J., Matthews, G.J., Baumer, B.S., “How often does the best team win? A unified approach to understanding randomness in North American sport,” The Annals of Applied Statistics, vol. 12, no. 4, 2018, pp. 2483–2516. URL (https://doi.org/10.1214/18-AOAS1165)

To cite this package in your work, see:

citation("teamcolors")

Notes

teamcolors's People

Contributors

adror1 avatar allanbutler avatar beanumber avatar clarajrosenberg avatar darh78 avatar gjm112 avatar lbenz730 avatar martinle5 avatar pep93 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

teamcolors's Issues

Canadian football

I want to add colours for the Canadian Football League, but I have a few questions on the formatting.

  1. Some teams (Montreal and Ottawa) have two names - an English name and a French name. Should I just add the English language name?
  2. The league has many defunct teams (mostly due to the failed expansion into the US during the 1994-95) and different franchises existing at different times with the same name and city. How do you handle defunct teams? Is it safe to assume due the the lack of Hartford Whalers in the database that they are ignored?

Cheers

object ‘teamcolors’ is not exported by 'namespace:teamcolors'

Hello teamcolors contributors,

I'm developing an R package (bbgraphsR) that uses the teamcolors dataframe from your package in one of my functions (viz_standings), but either when running devtools::load_all() or devtools::check(), I receive the following warning:

Warning: object ‘teamcolors’ is not exported by 'namespace:teamcolors'

I indeed checked the NAMESPACE file and it makes sense because there is no line exporting that table.

In that case, can I save the table data and use it in my package?
Or is it expected to export that table in the future?

Thanks in advance for your feedback.

Regards.

Daniel.

Update some team names

Change Washington Redskins to Washington Commanders
Change Cleveland Indians to Cleveland Guardians

College Conference Realignment

https://www.espn.com/college-football/story/_/id/35804245/cfb-conference-realignment-tracker-2023

We need to update a bunch of college team divisions.
2022 changes:
Marshall, Old Dominion and Southern Miss to Sun Belt.

2023 changes:
BYU, Cincinnati, Houston to Big 12
Charlotte, Florida Atlantic, North Texas, Rice, UAB, UTSA to AAC.
Jacksonville State, Liberty, New Mexico St, Sam Houston to CUSA

2024 changes:
Cal, SMU and Stanford to ACC.
Oregon, UCLA, USC, Washington to Big 10.
Arizona St, Arizona, Colorado, Utah to Big 12.
Texas and Oklahoma to SEC.
Kennessaw State to CUSA.

`scale_color_teams` behavior has changed (?)

Hi, this question is either a bug or user error or both, so I wasn't sure whether to post it as an issue. Apologies if it is user error.

As recently as May 2021, I was able to change the labels on my ggplot with scale_color_teams(labels = my_vector_of_labels).

Here's a toy example and my work-around:

library(ggplot2)
library(teamcolors)

#Example data; subset of data from bkref.com
toy_data = data.frame(
  team = rep(c("BOS", "SAS"), times = 3), 
  year = rep(2014:2016, each = 2), 
  Win_Pct = c(0.305, 0.756, 0.488, 0.671, 0.585, 0.817), 
  teamname = rep(c("Boston Celtics", "San Antonio Spurs"), times = 3)
)

# Base plot
# Original plot used 37 seasons of data and all 30 teams, so shrinking the label size was critical
p = ggplot(toy_data, aes(x = year, y = Win_Pct, color = teamname)) + 
  geom_line(size = 5) # line size for clarity of issue
p

image

# What worked as recently as May 2021, but now does not
p + scale_color_teams(1, labels = toy_data$team)

image

# How the preceding plot was intended to look
p + scale_color_discrete(labels = toy_data$team, type = league_pal(lg = "nba"))

image

Add NCAA Colors

Team Colors website updated to have NCAA teams. Any chance we can get these into the package as well? Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.