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.
To install the CRAN version, use:
install.packages(teamcolors)
To install the development version from GitHub, use:
devtools::install_github("beanumber/teamcolors")
library(teamcolors) head(teamcolors)
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")
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()
pythag <- pythag %>% left_join(teamcolors, by = "name") with(pythag, plot(wpct, exp_wpct, bg = primary, col = secondary, pch = 21, cex = 3))
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()
To view color palettes for college teams, use the show_ncaa_col()
function.
show_ncaa_col()
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)) )
For more examples see:
To cite this package in your work, see:
citation("teamcolors")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.