View source: R/nfl_team_factors.R
nfl_team_factor | R Documentation |
Create Ordered NFL Team Name Factor
nfl_team_factor(teams, ...)
teams |
A vector of NFL team abbreviations that should be included in
|
... |
One or more unquoted column names of |
Object of class "factor"
# unsorted vector including NFL team abbreviations
teams <- c("LAC", "LV", "CLE", "BAL", "DEN", "PIT", "CIN", "KC")
# defaults to sort by division and nick name in ascending order
nfl_team_factor(teams)
# can sort by every column in nflreadr::load_teams()
nfl_team_factor(teams, team_abbr)
# descending order by using base::rev()
nfl_team_factor(teams, rev(team_abbr))
######### HOW TO USE IN PRACTICE #########
library(ggplot2)
# load some sample data from the ggplot2 package
plot_data <- mpg
# add a new column by randomly sampling the above defined teams vector
plot_data$team <- sample(teams, nrow(mpg), replace = TRUE)
# Now we plot the data and facet by team abbreviation. ggplot automatically
# converts the team names to a factor and sorts it alphabetically
ggplot(plot_data, aes(displ, hwy)) +
geom_point() +
facet_wrap(~team, ncol = 4) +
theme_minimal()
# We'll change the order of facets by making another team name column and
# converting it to an ordered factor. Again, this defaults to sort by division
# and nick name in ascending order.
plot_data$ordered_team <- nfl_team_factor(sample(teams, nrow(mpg), replace = TRUE))
# Let's check how the facets are ordered now.
ggplot(plot_data, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ordered_team, ncol = 4) +
theme_minimal()
# The facet order looks weird because the defaults is meant to be used with
# NFL team wordmarks. So let's use the actual wordmarks and look at the result.
ggplot(plot_data, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ordered_team, ncol = 4) +
theme_minimal() +
theme(strip.text = element_nfl_wordmark())
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.