-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-data-viz.qmd
261 lines (201 loc) · 7.1 KB
/
05-data-viz.qmd
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# Data Exploration and Visualization
## Stacked ggplot with custom periodization colors
How to create a stacked bar chart to see your gps for the week. Label with your morphocycle/tactical periodization labels. And as a kicker - start your week on Thursday since that is your first game. Final output and code will look like this:
```{r final-first}
#| code-fold: true
#| warning: false
library(lubridate)
library(tibble)
library(dplyr)
library(ggplot2)
library(gt)
# DATA -----------------------------------------------------------------------------------------------------------------
# Create our own data
raw_gps_df <- tibble(
date = seq.Date(from = as.Date('2023-08-01'), to = as.Date('2023-08-14'),
by = 'days'),
metric = c(0, 8, 12, 18, 6, 12, 20, 0, 6, 9, 7, 19, 4, 17)
)
raw_session_desc <- tibble(
date = seq.Date(from = as.Date('2023-08-01'), to = as.Date('2023-08-14'),
by = 'days'),
tp_code = c('Off', 'ReEntry', 'Strength', 'Match 1', 'Recovery', 'Activation',
'Match 2', 'Off', 'Strength', 'Speed', 'Activation', 'Match 1',
'ReEntry-Activation', 'Match 2')
)
# Color codes. Presented a little cleaner to read (for us humans)
raw_tp_codes <-tribble(
~tp_code, ~tp_color,
'Off', 'grey',
'ReEntry', 'lightgreen',
'Strength', 'blue',
'Endurance', 'limegreen',
'Speed', 'yellow',
'Activation', '#fff5a9',
'ReEntry-Activation','lightgreen',
'Match 1', 'darkgreen',
'Match 2', 'darkgreen',
'Recovery', 'chartreuse'
)
# CUSTOM FUNCTION ------------------------------------------------------------------------------------------------------
# Custom function to change a day ("Sunday", "Monday", "Tuesday", etc)
# into a number with Sun = 1. Surprised this isn't a function in lubridate.
daynum <- function(day){
day <- tolower(day)
if(grepl("su", day)) {
return(1)
} else if (grepl("mo", day)) {
return(2)
} else if (grepl("tu", day)) {
return(3)
} else if (grepl("we", day)) {
return(4)
} else if (grepl("th", day)) {
return(5)
} else if (grepl("fr", day)) {
return(6)
} else if (grepl("sa", day)) {
return(7)
} else {
return(NA)
}
}
# This is the order we want our week to be in.
# Thursday is the first day, Wednesday is the last.
week_order <- c('Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed' )
# TRANSFORM ------------------------------------------------------------------------------------------------------------
# Combine into one data frame
session_df <- raw_gps_df |>
left_join(raw_session_desc, by = 'date') |>
left_join(raw_tp_codes, by = 'tp_code') |>
mutate(week = floor_date(date, unit = 'weeks', daynum(week_order[1])))
cols <- distinct(session_df, tp_code, tp_color) |> deframe()
# PLOT -----------------------------------------------------------------------------------------------------------------
session_df |>
ggplot(aes(x = week, y = metric, fill = tp_code, group = date)) +
geom_col(position = position_stack(reverse = TRUE)) +
scale_fill_manual(
name = "Tact Periodization Days",
values = cols,
) +
scale_x_date(
breaks = session_df$week,
date_labels = "%m-%d") +
theme_minimal()
```
First lets create some fake data to use. You would probably import these with `read_csv`, but I'm going to keep it simple and just create it here.
```{r}
#| warning: false
library(lubridate)
library(tibble)
library(dplyr)
library(ggplot2)
library(gt)
# Create our own data
raw_gps_df <- tibble(
date = seq.Date(from = as.Date('2023-08-01'), to = as.Date('2023-08-14'),
by = 'days'),
metric = c(0, 8, 12, 18, 6, 12, 20, 0, 6, 9, 7, 19, 4, 17)
)
raw_session_desc <- tibble(
date = seq.Date(from = as.Date('2023-08-01'), to = as.Date('2023-08-14'),
by = 'days'),
tp_code = c('Off', 'ReEntry', 'Strength', 'Match 1', 'Recovery', 'Activation',
'Match 2', 'Off', 'Strength', 'Speed', 'Activation', 'Match 1',
'ReEntry-Activation', 'Match 2')
)
# Look at the data
raw_gps_df |> gt()
raw_session_desc |> gt()
```
I usually import my data as `raw_data <- ...` as it's a pain to trouble shoot and have to and have to to rerun the import from the top of the code.
```{r}
# Combine data
session_df <- raw_gps_df |>
left_join(raw_session_desc, by = 'date')
# View combined data
session_df |> head() |> gt()
```
```{r}
# Plot data
session_df |>
ggplot(aes(x = date, y = metric, fill = tp_code)) +
geom_col() +
theme_minimal()
```
It would be nice to use our own custom color scheme. Help on using custom color and labels in ggplot found [here](https://stackoverflow.com/questions/68557812/use-custom-color-and-custom-label-from-dataframe-columns-in-ggplot2). The 'trick' is to use `deframe`, which converts a two-column data frames to a named vector or list, using the first column as name and the second column as value. If the input has only one column, an unnamed vector is returned
```{r}
# Color codes. Presented a little cleaner to read (for us humans)
raw_tp_codes <-tribble(
~tp_code, ~tp_color,
'Off', 'grey',
'ReEntry', 'lightgreen',
'Strength', 'blue',
'Endurance', 'limegreen',
'Speed', 'yellow',
'Activation', '#fff5a9',
'ReEntry-Activation','lightgreen',
'Match 1', 'darkgreen',
'Match 2', 'darkgreen',
'Recovery', 'chartreuse'
)
# Import and combine data
session_df <- raw_gps_df |>
left_join(raw_session_desc, by = 'date')
# deframe() converts two-column data frames to a named vector or list
cols <- distinct(raw_tp_codes, tp_code, tp_color) |> deframe()
# Following not used but may come in handy in the future
# labs \<- distinct(raw_tp_codes, tp_code, tp_code) |> deframe()
# PLot
session_df |>
ggplot(aes(x = date, y = metric, fill = tp_code)) +
geom_col(position='stack', stat='identity') +
scale_color_manual(aesthetics = 'fill',
values = cols
) +
theme_minimal()
```
```{r}
# Custom function to change a day ("Sunday", "Monday", "Tuesday", etc)
# into a number with Sun = 1. Surprised this isn't a function in lubridate.
daynum <- function(day){
day <- tolower(day)
if(grepl("su", day)) {
return(1)
} else if (grepl("mo", day)) {
return(2)
} else if (grepl("tu", day)) {
return(3)
} else if (grepl("we", day)) {
return(4)
} else if (grepl("th", day)) {
return(5)
} else if (grepl("fr", day)) {
return(6)
} else if (grepl("sa", day)) {
return(7)
} else {
return(NA)
}
}
# This is the order we want our week to be in.
# Thursday is the first day, Wednesday is the last.
week_order <- c('Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed' )
# Combine
session_df <- raw_gps_df |>
left_join(raw_session_desc, by = 'date') |>
left_join(raw_tp_codes, by = 'tp_code') |>
mutate(week = floor_date(date, unit = 'weeks', daynum(week_order[1])))
cols <- distinct(session_df, tp_code, tp_color) |> deframe()
session_df |>
ggplot(aes(x = week, y = metric, fill = tp_code, group = date)) +
geom_col(position = position_stack(reverse = TRUE)) +
scale_fill_manual(
name = "Tact Periodization Days",
values = cols,
) +
scale_x_date(
breaks = session_df$week,
date_labels = "%m-%d") +
theme_minimal()
```