forked from beanumber/teamcolors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
76 lines (62 loc) · 2.38 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---
title: "teamcolors"
output:
github_document:
html_preview: true
---
[![Travis-CI Build Status](https://travis-ci.org/beanumber/teamcolors.svg?branch=master)](https://travis-ci.org/beanumber/teamcolors)
An R package providing color palettes for pro sports teams. The palattes are provided by [Jim Neilsen's Team Colors website](http://jim-nielsen.com/teamcolors/).
## Install
```{r, eval=FALSE}
devtools::install_github("beanumber/teamcolors")
```
## Load
```{r}
library(teamcolors)
head(teamcolors)
```
## Plot
```{r base-r, message=FALSE}
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)) %>%
left_join(teamcolors, by = "name")
with(pythag, plot(wpct, exp_wpct, bg = primary, col = secondary, pch = 21, cex = 3))
```
```{r ggplot, message=FALSE, warning=FALSE}
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_manual(values = pythag$primary, guide = FALSE) +
scale_color_manual(values = pythag$secondary, 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(pythag$yearID[1], "MLB Season", sep = " "),
caption = "Source: the Lahman baseball database. Using teamcolors R pckg") +
coord_equal()
```
## Key
```{r teamcolors, fig.height=10}
teams <- teamcolors %>%
filter(league %in% c("mlb", "nba", "nfl", "nhl"))
ggplot(teams, aes(x = name, color = name, fill = name)) +
geom_bar() +
facet_wrap(~toupper(league), scales = "free_y") +
coord_flip() +
scale_x_discrete(NULL) +
scale_y_continuous(NULL) +
scale_fill_manual(values = teams$primary) +
scale_color_manual(values = teams$secondary) +
guides(color = FALSE, fill = FALSE) +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank())
```
## 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," (https://arxiv.org/abs/1701.05976)