Skip to content

Instantly share code, notes, and snippets.

@batpigandme
Last active January 29, 2016 15:23
Show Gist options
  • Save batpigandme/d9a877e33f0cac73335c to your computer and use it in GitHub Desktop.
Save batpigandme/d9a877e33f0cac73335c to your computer and use it in GitHub Desktop.
Different ways of visualizing team point leads and outcomes using small multiples.
## visualize team point leads and outcomes with small multiples
## install and load ggplot2
install.packages("ggplot2")
library(ggplot2)
## make separate point plots for each team
ggplot(teamgame_point_leads, aes(x=final_point_diff, y=points_biggest_lead)) + geom_point() +
facet_wrap(~team_slug)
## make histograms of point leads by team
## set bins (optional, but useful)
bins <- c(0, 5, 10, 15, 20, 25, 30, 35)
## specify data, variable, and group
ggplot(data = teamgame_point_leads, aes(x=points_biggest_lead)) +
facet_wrap(~team_slug) +
geom_histogram(stat="bin", breaks=bins)
## play around with params to make it nicer looking
ggplot(data = teamgame_point_leads, aes(x=points_biggest_lead)) +
facet_wrap(~team_slug) +
geom_histogram(stat="bin", breaks=bins, fill="#FFFFFF", colour="black") +
ggtitle("NBA Team Biggest Leads") +
theme(plot.title = element_text(family="Trebuchet MS", face="bold", size=20, hjust=0, color="#555555")) +
theme(text = element_text(face="bold", size = 12)) +
theme(axis.text.x = element_text(angle=90, size = 7)) +
labs(x = "points biggest lead", y = "count of games") +
theme(axis.text.y = element_text(size=7))
## box plot for each team by outcome
ggplot(teamgame_point_leads, aes(x=team_outcome, y=points_biggest_lead, fill=team_outcome)) +
geom_boxplot() +
facet_wrap(~team_slug) +
theme(plot.title = element_text(size=20, hjust=0, color="#555555")) +
theme(text = element_text(face="bold", size = 12)) +
theme(axis.text.x = element_text(angle=90, size = 7)) +
theme(axis.text.y = element_text(size=8))
## specify parameters to change colors, keys, titles
ggplot(teamgame_point_leads, aes(x=team_outcome, y=points_biggest_lead, fill=team_outcome)) +
geom_boxplot(outlier.shape = 3) +
guides(fill=FALSE) +
facet_wrap(~team_slug) +
ggtitle("NBA Team Point Leads by Outcome") +
theme(plot.title = element_text(family="Trebuchet MS", face="bold", size=20, hjust=0, color="#555555")) +
theme(text = element_text(face="bold", size = 12)) +
theme(axis.text.x = element_text(angle=90, size = 7)) +
labs(x = "outcome", y = "points biggest lead") +
theme(axis.text.y = element_text(size=8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment