Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spin it #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
Binary file added 2019-04-09/my_tennis_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 30 additions & 22 deletions 2019-04-09/tidy_tuesday_4_9_19.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Tidy Tuesday 4/9/2019
# Allison Horst
# Tennis Grand Slam Champions
#' ---
#' title: Tidy Tuesday 4/9/2019<br> Tennis Grand Slam Champions
#' author: Allison Horst
#' output: github_document
#' ---

#+ message = FALSE, warning = FALSE
library(tidyverse)
library(RColorBrewer)
library(wesanderson)
Expand All @@ -11,14 +14,14 @@ library(LaCroixColoR)

# font_import()

# Get data:
# player_dob <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-09/player_dob.csv")
#' Get data:

# grand_slams <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-09/grand_slams.csv")
#+ message = FALSE
player_dob <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-09/player_dob.csv")
grand_slams <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-09/grand_slams.csv")
grand_slam_timeline <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-09/grand_slam_timeline.csv")

# grand_slam_timeline <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-09/grand_slam_timeline.csv")

# This seems like excessive grouping and ungrouping, mer?
#' This seems like excessive grouping and ungrouping, mer?

past_qual <- c("Won","Finalist","Semi-finalist","Quarterfinalist", "4th Round", "3rd Round", "2nd Round", "1st Round")

Expand All @@ -30,13 +33,13 @@ sum_table <- grand_slam_timeline %>%
group_by(gender) %>%
group_split() # This is pretty cool! First time using group_split()

# Access the separate tibbles (probably bad practice, but wanted to try using group_splitanyway...)
#' Access the separate tibbles (probably bad practice, but wanted to try using group_splitanyway...)

male_players <- sum_table[[2]]
female_players <- sum_table[[1]]


# Top 20 males by # appearances after qualification (doesn't include absence/retire data)
#' Top 20 males by # appearances after qualification (doesn't include absence/retire data)
top_male_appear <- male_players %>%
group_by(player) %>%
summarize(
Expand All @@ -45,7 +48,7 @@ top_male_appear <- male_players %>%
arrange(-appearances) %>%
head(20)

# Top females by # appearances after qualification (doesn't include absence/retire data)
#' Top females by # appearances after qualification (doesn't include absence/retire data)
top_female_appear <- female_players %>%
group_by(player) %>%
summarize(
Expand All @@ -57,7 +60,7 @@ top_female_appear <- female_players %>%
vec_m <- unique(top_male_appear$player)
vec_f <- unique(top_female_appear$player)

# Joins to keep top 10 by total appearances beyond qualifying round
#' Joins to keep top 10 by total appearances beyond qualifying round

m_appear <- top_male_appear %>%
inner_join(male_players) %>%
Expand All @@ -69,36 +72,40 @@ f_appear <- top_female_appear %>%
mutate(player = as.factor(player)) %>%
mutate(outcome = as.factor(outcome))

# Relevel by top players
#' Relevel by top players
f_appear$player <- fct_relevel(f_appear$player, vec_f)
m_appear$player <- fct_relevel(m_appear$player, vec_m)

# Relevel outcome
#' Relevel outcome
f_appear$outcome <- fct_relevel(f_appear$outcome,"Won", "Finalist","Semi-finalist","Quarterfinalist","4th Round", "3rd Round","2nd Round","1st Round")

m_appear$outcome <- fct_relevel(m_appear$outcome,"Won", "Finalist","Semi-finalist","Quarterfinalist","4th Round", "3rd Round","2nd Round","1st Round")

# Then make a graph that shows the level reached after qualifiers for each:
#' Then make a graph that shows the level reached after qualifiers for each:

#' Create palette:
# pal <- wes_palette(8, name = "FantasticFox1", type = "continuous")
# Create palette:
pal <- lacroix_palette("PassionFruit", n = 8, type = "continuous")

# Graph of female top appearances:
#' Graph of female top appearances:
#+ fig.retina = 3, fig.width = 8
ggplot(f_appear, aes(x = reorder(player, desc(player)), y = n)) +
geom_col(aes(fill = outcome)) +
theme_pomological(base_family = "Courier New", base_size = 12) +
scale_x_discrete(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0), limits = c(0,90)) +
scale_fill_manual(values = pal, name = "Outcome:") +
labs(x = "", y = "Number of appearances\n(beyond qualifiers)", title = "Top Grand Slam appearances & outcomes") +
theme(legend.position = "bottom", axis.text.x=element_text(size=11, face = "bold", hjust = 1), axis.text.y = element_text(size = 10, color = "slateblue4")) +
theme(legend.position = "bottom",
legend.justification='left',
axis.text.x=element_text(size=11, face = "bold", hjust = 1), axis.text.y = element_text(size = 10, color = "slateblue4")) +
coord_flip()

# Save it:
#' Save it:
ggsave("my_tennis_plot.png", width = 8, height = 7)

# Male version:
#' Male version:
#+ fig.retina = 3, fig.width = 8
ggplot(m_appear, aes(x = reorder(player, desc(player)), y = n)) +
geom_col(aes(fill = outcome)) +
theme_pomological(base_family = "Courier New",
Expand All @@ -109,7 +116,8 @@ ggplot(m_appear, aes(x = reorder(player, desc(player)), y = n)) +
scale_fill_manual(values = pal,
name = "Outcome:") +
labs(x = "", y = "Number of appearances", title = "Grand Slam Appearances Colorblast") +
theme(legend.position = "bottom",
theme(legend.position="bottom",
legend.justification = "left",
axis.text.x=element_text(size=10, face = "bold", angle = 50, hjust = 1)) +
coord_flip()

Expand Down
165 changes: 165 additions & 0 deletions 2019-04-09/tidy_tuesday_4_9_19.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
Tidy Tuesday 4/9/2019<br> Tennis Grand Slam Champions
================
Allison Horst
2019-04-10

``` r
library(tidyverse)
library(RColorBrewer)
library(wesanderson)
library(ggpomological)
library(extrafont)
library(LaCroixColoR)

# font_import()
```

Get
data:

``` r
player_dob <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-09/player_dob.csv")
grand_slams <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-09/grand_slams.csv")
grand_slam_timeline <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-09/grand_slam_timeline.csv")
```

This seems like excessive grouping and ungrouping,
mer?

``` r
past_qual <- c("Won","Finalist","Semi-finalist","Quarterfinalist", "4th Round", "3rd Round", "2nd Round", "1st Round")

sum_table <- grand_slam_timeline %>%
filter(!is.na(outcome), outcome %in% past_qual) %>%
group_by(player, gender, outcome) %>%
tally() %>%
ungroup() %>%
group_by(gender) %>%
group_split() # This is pretty cool! First time using group_split()
```

Access the separate tibbles (probably bad practice, but wanted to try
using group\_splitanyway…)

``` r
male_players <- sum_table[[2]]
female_players <- sum_table[[1]]
```

Top 20 males by \# appearances after qualification (doesn’t include
absence/retire data)

``` r
top_male_appear <- male_players %>%
group_by(player) %>%
summarize(
appearances = sum(n)
) %>%
arrange(-appearances) %>%
head(20)
```

Top females by \# appearances after qualification (doesn’t include
absence/retire data)

``` r
top_female_appear <- female_players %>%
group_by(player) %>%
summarize(
appearances = sum(n)
) %>%
arrange(-appearances) %>%
head(20)

vec_m <- unique(top_male_appear$player)
vec_f <- unique(top_female_appear$player)
```

Joins to keep top 10 by total appearances beyond qualifying round

``` r
m_appear <- top_male_appear %>%
inner_join(male_players) %>%
mutate(player = as.factor(player)) %>%
mutate(outcome = as.factor(outcome))
```

## Joining, by = "player"

``` r
f_appear <- top_female_appear %>%
inner_join(female_players) %>%
mutate(player = as.factor(player)) %>%
mutate(outcome = as.factor(outcome))
```

## Joining, by = "player"

Relevel by top players

``` r
f_appear$player <- fct_relevel(f_appear$player, vec_f)
m_appear$player <- fct_relevel(m_appear$player, vec_m)
```

Relevel
outcome

``` r
f_appear$outcome <- fct_relevel(f_appear$outcome,"Won", "Finalist","Semi-finalist","Quarterfinalist","4th Round", "3rd Round","2nd Round","1st Round")

m_appear$outcome <- fct_relevel(m_appear$outcome,"Won", "Finalist","Semi-finalist","Quarterfinalist","4th Round", "3rd Round","2nd Round","1st Round")
```

Then make a graph that shows the level reached after qualifiers for
each: Create palette:

``` r
# pal <- wes_palette(8, name = "FantasticFox1", type = "continuous")
pal <- lacroix_palette("PassionFruit", n = 8, type = "continuous")
```

Graph of female top appearances:

``` r
ggplot(f_appear, aes(x = reorder(player, desc(player)), y = n)) +
geom_col(aes(fill = outcome)) +
theme_pomological(base_family = "Courier New", base_size = 12) +
scale_x_discrete(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0), limits = c(0,90)) +
scale_fill_manual(values = pal, name = "Outcome:") +
labs(x = "", y = "Number of appearances\n(beyond qualifiers)", title = "Top Grand Slam appearances & outcomes") +
theme(legend.position = "bottom",
legend.justification='left',
axis.text.x=element_text(size=11, face = "bold", hjust = 1), axis.text.y = element_text(size = 10, color = "slateblue4")) +
coord_flip()
```

<img src="tidy_tuesday_4_9_19_files/figure-gfm/unnamed-chunk-11-1.png" width="768" />

Save it:

``` r
ggsave("my_tennis_plot.png", width = 8, height = 7)
```

Male version:

``` r
ggplot(m_appear, aes(x = reorder(player, desc(player)), y = n)) +
geom_col(aes(fill = outcome)) +
theme_pomological(base_family = "Courier New",
base_size = 12) +
scale_x_discrete(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0),
limits = c(0,80)) +
scale_fill_manual(values = pal,
name = "Outcome:") +
labs(x = "", y = "Number of appearances", title = "Grand Slam Appearances Colorblast") +
theme(legend.position="bottom",
legend.justification = "left",
axis.text.x=element_text(size=10, face = "bold", angle = 50, hjust = 1)) +
coord_flip()
```

<img src="tidy_tuesday_4_9_19_files/figure-gfm/unnamed-chunk-13-1.png" width="768" />
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions allison-tidy-tuesdays.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: XeLaTeX

AutoAppendNewline: Yes
Binary file added my_tennis_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.