Show code
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
tidyverse,
tidytuesdayR,
ggtext,
showtext,
janitor, #for clean_names()
scales,
glue,
here
)
font_add_google('Lexend')
showtext_auto()
December 30, 2022
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
tidyverse,
tidytuesdayR,
ggtext,
showtext,
janitor, #for clean_names()
scales,
glue,
here
)
font_add_google('Lexend')
showtext_auto()
tt_year <- 2020
tt_week <- 41
tuesdata <- tidytuesdayR::tt_load(tt_year, week = tt_week)
tournament <- tuesdata$tournament
tournament %>%
glimpse()
Rows: 2,092
Columns: 19
$ year <dbl> 1982, 1982, 1982, 1982, 1982, 1982, 1982, 1982, 1982…
$ school <chr> "Arizona St.", "Auburn", "Cheyney", "Clemson", "Drak…
$ seed <dbl> 4, 7, 2, 5, 4, 6, 5, 8, 7, 7, 4, 8, 2, 1, 1, 2, 3, 6…
$ conference <chr> "Western Collegiate", "Southeastern", "Independent",…
$ conf_w <dbl> NA, NA, NA, 6, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ conf_l <dbl> NA, NA, NA, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ conf_percent <dbl> NA, NA, NA, 66.7, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ conf_place <chr> "-", "-", "-", "4th", "-", "-", "-", "-", "-", "-", …
$ reg_w <dbl> 23, 24, 24, 20, 26, 19, 21, 14, 21, 28, 24, 17, 22, …
$ reg_l <dbl> 6, 4, 2, 11, 6, 7, 8, 10, 8, 7, 5, 13, 7, 5, 1, 6, 4…
$ reg_percent <dbl> 79.3, 85.7, 92.3, 64.5, 81.3, 73.1, 72.4, 58.3, 72.4…
$ how_qual <chr> "at-large", "at-large", "at-large", "at-large", "aut…
$ x1st_game_at_home <chr> "Y", "N", "Y", "N", "Y", "N", "N", "N", "N", "N", "Y…
$ tourney_w <dbl> 1, 0, 4, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 1, 5, 3, 1, 1…
$ tourney_l <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1…
$ tourney_finish <chr> "RSF", "1st", "N2nd", "1st", "RF", "1st", "1st", "1s…
$ full_w <dbl> 24, 24, 28, 20, 28, 19, 21, 14, 21, 28, 26, 17, 24, …
$ full_l <dbl> 7, 5, 3, 12, 7, 8, 9, 11, 9, 8, 6, 14, 8, 6, 1, 7, 5…
$ full_percent <dbl> 77.4, 82.8, 90.3, 62.5, 80.0, 70.4, 70.0, 56.0, 70.0…
my_theme <- theme(
# choose font family
text = element_text(family = 'Lexend', color='black', size=14),
plot.background = element_rect(color='white'),
panel.background = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
legend.position = 'none',
strip.background = element_blank())
big12_viz <- tournament %>%
filter(conference == 'Big 12',
!school %in% c('Colorado', 'Missouri', 'Nebraska', 'Texas A&M')) %>%
ggplot(.,
aes(x=year,
y=reg_percent/100)) +
geom_line() +
geom_hline(yintercept = tournament$median_w_l[1]/100) +
geom_ribbon(aes(ymin=median_w_l/100, ymax=full_percent/100),
fill='firebrick1') +
geom_ribbon(aes(ymin=median_w_l/100, ymax=above_below_median/100),
fill='springgreen3') +
facet_wrap(~ school) +
ggtitle("Baylor has dominated the Big 12",
subtitle = "Plot compares season-by-season win percentage of Big 12 programs in the regular season compared with the historical median win percentage of all programs in women's basketball<br>") +
labs(x='Year',
y='',
# y='Win % by season compared to\nhistorical median for all programs\n',
caption='Tidy Tuesday Week 41 (2020)<br>**Source**: FiveThirtyEight') +
scale_y_continuous(labels = percent) +
my_theme +
theme(plot.title = element_textbox(size=rel(3), face='bold'),
plot.subtitle = element_textbox_simple(size=rel(1.1), lineheight=.3),
plot.caption = element_textbox(lineheight=.3),
strip.text = element_textbox(size=rel(1.1), face='bold'))
# Save the plot as PNG
ggsave(
filename = glue("tt_{tt_year}_{tt_week}.png"),
plot = big12_viz,
width = 4, height = 3, units = "in", dpi = 320
)
# make thumbnail for page
magick::image_read(glue("tt_{tt_year}_{tt_week}.png")) %>%
magick::image_resize(geometry = "400") %>%
magick::image_write(glue("tt_{tt_year}_{tt_week}_thumbnail.png"))
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.6.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: America/Denver
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] here_1.0.1 glue_1.8.0 scales_1.3.0 janitor_2.2.0
[5] showtext_0.9-7 showtextdb_3.0 sysfonts_0.8.9 ggtext_0.1.2
[9] tidytuesdayR_1.1.2 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1
[13] dplyr_1.1.4 purrr_1.0.2 readr_2.1.5 tidyr_1.3.1
[17] tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0 pacman_0.5.1
loaded via a namespace (and not attached):
[1] gtable_0.3.5 xfun_0.48 httr2_1.0.5 htmlwidgets_1.6.4
[5] gh_1.4.1 tzdb_0.4.0 vctrs_0.6.5 tools_4.4.0
[9] generics_0.1.3 curl_5.2.3 parallel_4.4.0 fansi_1.0.6
[13] pkgconfig_2.0.3 lifecycle_1.0.4 farver_2.1.2 compiler_4.4.0
[17] textshaping_0.4.0 munsell_0.5.1 snakecase_0.11.1 htmltools_0.5.8.1
[21] yaml_2.3.10 pillar_1.9.0 crayon_1.5.3 magick_2.8.5
[25] commonmark_1.9.2 tidyselect_1.2.1 digest_0.6.37 stringi_1.8.4
[29] labeling_0.4.3 rprojroot_2.0.4 fastmap_1.2.0 grid_4.4.0
[33] colorspace_2.1-1 cli_3.6.3 magrittr_2.0.3 utf8_1.2.4
[37] withr_3.0.1 rappdirs_0.3.3 bit64_4.5.2 timechange_0.3.0
[41] rmarkdown_2.28 gitcreds_0.1.2 bit_4.5.0 ragg_1.3.3
[45] hms_1.1.3 evaluate_1.0.1 knitr_1.48 markdown_1.13
[49] rlang_1.1.4 gridtext_0.1.5 Rcpp_1.0.13 xml2_1.3.6
[53] rstudioapi_0.17.1 vroom_1.6.5 jsonlite_1.8.9 R6_2.5.1
[57] systemfonts_1.1.0
---
title: "Visualizing Big 12 Basketball program performance"
description: "Which teams have reigned supreme?"
author:
- name: Mickey Rafa
url: https://mrafa3.github.io/
date: 12-30-2022
categories: [R, "#TidyTuesday", area-plot, small-multiples] # self-defined categories
image: "tt_2020_41_thumbnail.png"
draft: false # setting this to `true` will prevent your post from appearing on your listing page until you're ready!
format:
html:
toc: true
toc-depth: 5
code-link: true
code-fold: true
code-tools: true
code-summary: "Show code"
self-contained: true
editor_options:
chunk_output_type: inline
execute:
error: false
message: false
warning: false
eval: true
---
{#fig-1}
# 1. Load Packages & Setup
```{r setup, include=TRUE}
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
tidyverse,
tidytuesdayR,
ggtext,
showtext,
janitor, #for clean_names()
scales,
glue,
here
)
font_add_google('Lexend')
showtext_auto()
```
# 2. Read in the Data
```{r read_data, include=TRUE}
tt_year <- 2020
tt_week <- 41
tuesdata <- tidytuesdayR::tt_load(tt_year, week = tt_week)
tournament <- tuesdata$tournament
```
# 3. Examine the Data
```{r examine, include=TRUE, echo=TRUE}
tournament %>%
glimpse()
```
# 4. Tidy the Data
```{r tidy_tournament, include=TRUE}
tournament <- tournament %>%
mutate(median_w_l = median(reg_percent, na.rm = TRUE)) %>%
mutate(above_below_median = ifelse(reg_percent > median_w_l,
reg_percent, median_w_l))
```
# 5. Visualization Parameters
```{r my_theme, include=TRUE}
my_theme <- theme(
# choose font family
text = element_text(family = 'Lexend', color='black', size=14),
plot.background = element_rect(color='white'),
panel.background = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
legend.position = 'none',
strip.background = element_blank())
```
# 6. Plot
```{r big12_viz, fig.height=4, fig.width=4}
big12_viz <- tournament %>%
filter(conference == 'Big 12',
!school %in% c('Colorado', 'Missouri', 'Nebraska', 'Texas A&M')) %>%
ggplot(.,
aes(x=year,
y=reg_percent/100)) +
geom_line() +
geom_hline(yintercept = tournament$median_w_l[1]/100) +
geom_ribbon(aes(ymin=median_w_l/100, ymax=full_percent/100),
fill='firebrick1') +
geom_ribbon(aes(ymin=median_w_l/100, ymax=above_below_median/100),
fill='springgreen3') +
facet_wrap(~ school) +
ggtitle("Baylor has dominated the Big 12",
subtitle = "Plot compares season-by-season win percentage of Big 12 programs in the regular season compared with the historical median win percentage of all programs in women's basketball<br>") +
labs(x='Year',
y='',
# y='Win % by season compared to\nhistorical median for all programs\n',
caption='Tidy Tuesday Week 41 (2020)<br>**Source**: FiveThirtyEight') +
scale_y_continuous(labels = percent) +
my_theme +
theme(plot.title = element_textbox(size=rel(3), face='bold'),
plot.subtitle = element_textbox_simple(size=rel(1.1), lineheight=.3),
plot.caption = element_textbox(lineheight=.3),
strip.text = element_textbox(size=rel(1.1), face='bold'))
```
# 7. Save
```{r save_plot, include=TRUE}
# Save the plot as PNG
ggsave(
filename = glue("tt_{tt_year}_{tt_week}.png"),
plot = big12_viz,
width = 4, height = 3, units = "in", dpi = 320
)
# make thumbnail for page
magick::image_read(glue("tt_{tt_year}_{tt_week}.png")) %>%
magick::image_resize(geometry = "400") %>%
magick::image_write(glue("tt_{tt_year}_{tt_week}_thumbnail.png"))
```
# 8. Session Info
::: {.callout-tip collapse="true"}
##### Expand for Session Info
```{r, echo = FALSE}
sessionInfo()
```
:::
# 9. Github Repository
::: {.callout-tip collapse="true"}
##### Expand for GitHub Repo
[Access the GitHub repository here](https://github.com/mrafa3/mrafa3.github.io)
:::