-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_exercises.qmd
More file actions
115 lines (65 loc) · 2.99 KB
/
functions_exercises.qmd
File metadata and controls
115 lines (65 loc) · 2.99 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---
title: "Custom Functions Exercises"
date: "today"
format: html
editor: visual
---
## A. Workshop exercises
### A.1. Load tidyverse
```{r}
```
### A.2. Set themes
Run this chunk of code to set custom themes (`theme_bar` and `theme_line`) for our later graphics.
```{r}
theme_bar <- theme_minimal() +
theme(strip.text = element_text(size = 10.5, face = "bold",
color = "grey30", margin = margin(b=10)),
axis.text = element_text(size = 10),
axis.title.y = element_blank(),
axis.title.x = element_text(margin = margin(t=10), color = "grey30"),
axis.line.x = element_line(color = "grey30"),
panel.grid = element_blank(),
panel.grid.major.x = element_line(color = "grey80", linetype = "dotted"),
panel.spacing = unit(1,"cm"),
legend.title = element_blank())
theme_line <- theme_minimal() +
theme(axis.text = element_text(size = 10),
axis.title.x = element_blank(),
axis.title.y = element_text(margin = margin(r=10), color = "grey30"),
panel.grid = element_blank(),
panel.grid.major.y = element_line(color = "grey80"),
legend.title = element_blank())
```
### A.3. Load data
Today we'll be using a fake data set that includes student athlete GPAs from 2014 to 2024, broken down by sport and student status, sex, and race/ethnicity.
*Read in the "college_student_athletes.csv" file*
```{r}
```
### A.4. Aggregate
For future analysis we want four new data frames that include a **count of athletes** and their **average GPAs**:
1. by year and sport
2. by year, sport, and status
3. by year, sport, and sex
4. by year, sport, and race/ethnicity
*HINT: Use* `group_by()` *and* `summarize()`
```{r}
```
### A.5. Bar charts
Using a function, build a set of bar charts that show the number of athletes who played each sport in 2024 faceted by status, sex, then race/ethnicity. Your function should also allow you to highlight one or more sports using fill color.
```{r}
```
### A.6. Line chart
Create a line chart using the `year_sport` data frame that shows how average GPAs have changed over time by sport. Then grey out all the lines to prepare to highlight in the next step.
```{r}
```
Build a function that highlights one line at a time, keeping all other lines grey.
```{r}
```
## B. On your own
Use the aggregated data frames you created in section A.4. to build plots showing the breakdown of average GPA:
- faceted by status in 2024
- faceted by sex in 2020
- faceted by race in 2015
*Create the three plots listed above (they can be horizontal bar charts, but you might also try a dot plot using* `geom_point()` *instead). Test your code with the first one, then build a function to create the other two. HINT: Your function arguments should be the aggregated data frame, year, and grouping variable* (`Status` / `Sex` / `Race`).
```{r}
```