-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcylinder_working_tweet_version_oct2018.Rmd
413 lines (299 loc) · 14.3 KB
/
cylinder_working_tweet_version_oct2018.Rmd
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
---
title: "R Notebook"
output: html_notebook
---
```{r}
library(tidyverse); library(lubridate); library(gganimate)
set.seed(42)
sample <- tibble(
dates = seq.Date(from = ymd(20160701), to = ymd(20181001), by = "day"),
chg = runif(length(dates), min = -10, max = 10),
cuml = cumsum(chg)
)
```
Let's plot the series, along with a smoothed version. `geom_smooth` has a parameter called "span" that determines how much of the series should be averaged into each point.
```{r}
ggplot(sample, aes(dates, cuml)) +
geom_line(color = "gray70") +
geom_smooth(span = .1, se=F, method = "loess")
```
I'd like to animate how much smoothing there is.
* Possible to animate span parameter of geom_smooth directly?
* Alternatively, create new columns with smoothed data, gather, and transition those...
```{r}
# sample %>%
# mutate(smoothed = loess(cuml ~ dates), sample)
#
# ggplot(aes(dates, cuml)) +
# geom_line(color = "gray70") +
# geom_point() +
# # geom_smooth(span = .01, se=F, method = "loess") +
# # geom_smooth(span = .05, se=F, method = "loess") +
# # geom_smooth(span = .1, se=F, method = "loess") +
# # geom_smooth(span = .2, se=F, method = "loess") +
# # geom_smooth(span = .3, se=F, method = "loess") +
# # geom_smooth(span = .4, se=F, method = "loess") +
# transition_
# # transition_layers(layer_length = 1, transition_length = 1)
```
I'm curious what the series would look like if it appeared on the edge of a rotating cylinder, like a phonograph record or a seismograph. This will require a few translations:
* x axis should be wrapped around, so that each period (could be one day, or one week, or one month, or one year) is shown as one cycle. I think the mapping would be a sin function, so that the earliest values are mapped to x = 0, increasing linearly at first, until one quarter of the period (PI/2 in radians?), represented at max x.
* y could be adjustable, using height * -cos(phase), to allow for flat head-on viewing when height = 0, or some height to look at the cylinder from a different height.
```{r}
options(scipen = 99)
sample2 <- sample %>% # select(dates) %>%
mutate(dates_dec = decimal_date(dates),
dates_phase = (dates_dec %% 1),
dates_cycle_num = dates_dec - dates_phase,
dates_cyl = -sin(2*pi * dates_phase/ max(dates_phase))
)
a <- sample2 %>% # ggplot(aes(dates, dates_cyl)) +
ggplot(aes(dates_cyl, cuml)) +
geom_point(size = 0.1) +
geom_path(alpha = 0.3) +
transition_reveal(dates_cycle_num, dates_dec)
animate(a, nframes = 20)
```
To make this look cooler, I'd rather have the whole cylinder rotate, leaving the current point visible. This seems possible if we prepare the data first, calculating each frame and shfting the visibility and phase according to frame.
This worked well and I tweeted it out on 10/6/18.
```{r basically version that was 10-6-18 tweet}
library(tidyverse); library(lubridate); library(gganimate)
# Make some fake data. x will be
set.seed(42)
sample <- tibble(
dates = seq.Date(from = ymd(20160701), to = ymd(20181001), by = "day"),
chg = runif(length(dates), min = -10, max = 10),
value = cumsum(chg)
) %>% select(-chg)
frame_count <- (max(sample$dates) - min(sample$dates)) / lubridate::ddays(1)
sample3 <- map_df(seq_len(frame_count), ~sample, .id = "id") %>%
mutate(id = as.integer(id),
id_phase = (id %% 365) / 365) %>% arrange(dates) %>%
mutate(dates_dec = decimal_date(dates),
dates_num = dates_dec - min(dates_dec),
dates_prog = dates_num / max(dates_num),
dates_phase = (dates_dec %% 1),
relative_phase = id_phase - dates_phase,
dates_cycle_num = dates_dec - dates_phase,
dates_cyl = sin(2*pi * relative_phase/ max(relative_phase)),
nearness = -cos(2*pi * relative_phase/max(relative_phase))
) %>%
filter(dates_prog <= id / max(id)) %>%
group_by(id) %>%
mutate(recency = max(dates_dec) - dates_dec) %>% ungroup()
temp <- sample3 %>% # ggplot(aes(dates, dates_cyl)) +
filter(id > max(id) - 200)
b <- ggplot(temp, aes(dates_cyl, value, alpha = nearness, color = recency)) +
geom_path(aes(size = -recency)) +
scale_size(range = c(0,2)) +
transition_manual(id) + theme_void() +
guides(size = "none", alpha = "none", color = "none")
animate(b, nframes = 200, fps = 20, width = 300, height = 150) #, renderer = av_renderer())
```
```{r cleaned up version}
library(tidyverse); library(lubridate); library(gganimate)
# Fake data
set.seed(4.2)
sample <- tibble(
date = seq.Date(from = ymd(20160101), to = ymd(20181001), by = "day"),
chg = runif(length(date), min = -10, max = 10),
value = cumsum(chg)
) %>% select(-chg)
frame_count <- (max(sample$date) - min(sample$date)) / lubridate::ddays(1)
cycle_length <- 365
# From https://stackoverflow.com/a/8753732/6851825
# This copies the dataframe `frame_count` times, w/ one id number for each set
sample3 <- map_df(seq_len(frame_count), ~sample, .id = "id") %>%
mutate(id = as.integer(id)) %>%
# view_date is the "camera" date. We'll only keep data up to the camera date.
mutate(view_date = min(sample$date) + id - 1) %>%
filter(date <= view_date) %>%
mutate(days_ago = (view_date - date) / ddays(1),
phase_dif = (days_ago %% cycle_length) / cycle_length,
x_pos = -sin(2*pi * phase_dif),
nearness = cos(2*pi * phase_dif))
b <- ggplot(sample3) +
geom_path(aes(x_pos, value, alpha = nearness,
color = days_ago, size = -days_ago)) +
scale_size(range = c(0,2)) +
transition_manual(id) + theme_void() +
guides(size = "none", alpha = "none", color = "none")
# Change fps to 20+ for smoother motion
animate(b, fps = 25, duration = 15, width = 300, height = 150)
```
Getting closer. Something's a little buggy so lemme try rewriting another way
```{r}
library(tidyverse); library(lubridate); library(gganimate)
sample <- tibble(
dates = seq.Date(from = ymd(20160701), to = ymd(20181001), by = "day"),
chg = runif(length(dates), min = -10, max = 10),
cuml = cumsum(chg)
) %>% select(-chg)
# gall_att <- readr::read_csv("~/Downloads/gallery_attendance (1).csv") %>%
# mutate(dates = mdy(Date),
# paid = `Gallery Attendance` * `Paid %`)
source('~/R/r-orders/speedy_load.R')
gall_att <- daily_totals %>%
select(dates = perf_dt,
paid = Paid)
```
```{r Define Function add_cyclic_data}
add_cyclic_data <- function(df, cycle_length = 365) {
# df <- sample
# cycle_length <- 365
days_length <- (max(df$dates) - min(df$dates)) / lubridate::ddays(1)
df <- df %>% padr::pad()
df_longer <- map_df(seq_len(days_length), ~df, .id = "id") %>%
mutate(id = as.integer(id),
cur_date = min(df$dates) + id -1) %>%
filter(dates <= cur_date) %>%
mutate(days_ago = (cur_date - dates) / ddays(1),
phase_dif = (days_ago %% cycle_length) / cycle_length,
x_pos = -sin(2*pi * phase_dif/max(phase_dif)),
distance = -cos(2*pi * phase_dif/max(phase_dif)))
return(df_longer)
}
```
``` {r}
df_longer <- add_cyclic_data(sample %>% filter(dates > max(dates) - 900))
# cycle_length <- 365
b <- ggplot(df_longer, aes(x_pos, cuml, alpha = -distance, color = dates)) +
geom_path(aes(size = -days_ago)) +
# geom_vline(data = df_longer %>% filter(dates > max(dates) - cycle_length),
# aes(xintercept = x_pos), color = "gray50", alpha = 0.1) +
geom_vline(data = df_longer %>% filter(month(dates) %in% c(1, 4, 7, 10), day(dates) == 1),
aes(xintercept = x_pos, alpha = -distance), color = "gray50", lty = "dashed") +
scale_size(range = c(0,2)) +
transition_manual(id) + theme_void() +
guides(size = "none", alpha = "none", color = "none")
animate(b, nframes = 300, fps = 20, width = 300, height = 150) #, renderer = av_renderer())
```
```{r fig.width=10, height = 3}
###### Added later for smooth
gall_att2 <- gall_att %>%
mutate(day_num = (dates - min(dates))/ddays(1) + 1,
cuml_pd = cumsum(paid/1000),
days_7row = day_num - lag(day_num, 6),
cuml_7row = cuml_pd - lag(cuml_pd, 6),
avg = cuml_7row / days_7row) %>%
mutate(notes = case_when(dates == ymd(20160515) ~ "Opening",
dates == ymd(20160703) ~ "7/4",
dates == ymd(20160905) ~ "Labor\nDay",
dates == ymd(20161020) ~ "Rain",
dates == ymd(20161125) ~ "Thanksgiving",
dates == ymd(20161226) ~ "Xmas",
dates == ymd(20170218) ~ "Spring\nbreak",
dates == ymd(20170314) ~ "<-MD",
dates == ymd(20170528) ~ "MD->",
dates == ymd(20170624) ~ "<-Munch",
# dates == ymd(20170703) ~ "7/4",
dates == ymd(20170905) ~ "Labor\nDay",
dates == ymd(20171008) ~ "Munch->",
dates == ymd(20171125) ~ "Thanksgiving",
dates == ymd(20171226) ~ "Xmas",
dates == ymd(20180218) ~ "Spring\nbreak",
dates == ymd(20180519) ~ "Magritte",
dates == ymd(20180703) ~ "7/4",
dates == ymd(20180904) ~ "Labor\nDay",
TRUE ~ ""))
ggplot(gall_att2, aes(dates, avg)) + geom_line() +
geom_text(aes(label = notes, y = 2.5), hjust = 0)
```
```{r}
# att_longer <- add_cyclic_data(gall_att) %>%
att_longer <- add_cyclic_data(gall_att2, cycle_length = 364) %>%
filter(dates >= ymd(20160515),
paid > 0,
dates <= ymd(20181002),
cur_date <= ymd(20181002)
) %>%
filter(id > 10) %>%
mutate(avg = if_else(is.na(avg), lag(avg), avg))
(max(att_longer$dates) - min(att_longer$dates)) / lubridate::ddays(1)
max(att_longer$id) - min(att_longer$id)
seconds_per_mo = 1.5
fps = 20
b <- ggplot(att_longer, # %>% filter(cur_date < ymd(20170101)),
aes(x_pos, paid, alpha = -distance, color = days_ago)) +
# geom_vline(data = att_longer %>% filter(month(dates) == 7, day(dates) == 1, days_ago < 364),
# aes(xintercept = x_pos, alpha = -distance), color = "gray60", size = 0.4, lty = "dotted") +
geom_segment(data = att_longer %>% filter(day(dates) == 1, distance < -0.2, days_ago < 364),
aes(y = 0, yend = 3, xend = x_pos), color = "gray20", size = 0.5) +
# For some reason the month labels seemed to be off by one...
# Might have been time zone or something for day = 0
geom_text(data = att_longer %>% filter(day(dates) == 15, days_ago < 364, distance < -0.2),
aes(y = 0, label = month(dates, label = T, abbr = T), alpha = floor(-distance + 0.3)),
size = 6, color = "gray60", hjust = 0.5, vjust = 0) +
geom_text(data = att_longer %>% filter(distance < -0.25, days_ago < 330),
aes(y = 4000, label = str_wrap(notes, 13), alpha = floor(-distance + 0.3)),
size = 6, color = "gray60", hjust = 0.5, vjust = 0, family = "SFMOMA Text Offc") +
geom_path(aes(size = -days_ago)) +
scale_size(range = c(0.1,1)) +
scale_color_gradient2(low = "yellow", mid = "slateblue", midpoint = 364, high = "firebrick4") +
# scale_color_viridis_c(direction = -1, option = "A") +
transition_manual(id) +
theme_void() + theme(plot.background = element_rect(fill="black")) +
guides(size = "none", alpha = "none", color = "none")
animate(b, nframes = 870, duration = 60, width = 600, height = 400) #, renderer = av_renderer())
# animate(b, nframes = 100, fps = 20, width = 300, height = 150, renderer = ffmpeg_renderer(format = "mp4", ffmpeg = NULL, options = list(pix_fmt = "yuv420p")))
```
```{r}
#Plot weekly avgs
att_longer <- add_cyclic_data(gall_att) %>%
filter(dates >= ymd(20160515),
dates <= ymd(20181002),
cur_date <= ymd(20181002)) %>%
```
# Storms version
Perhaps the same technique could be applied to a standard R data set, like dplyr's storms.
```{r}
library(lubridate)
storms %>%
mutate(dt = ymd_h(paste(2018,month,day,hour)) +
if_else(month < 4, dyears(1), 0)) %>%
mutate(yr_name = paste(year, name)) %>%
group_by(yr_name) %>%
mutate(max_wind = max(wind)) %>%
ungroup() %>%
filter(max_wind > 110) %>%
# select(-(year:hour)) %>%
ggplot(aes(dt, lat, group = yr_name, color = lat)) +
geom_path(size = 0.5)
nasa %>%
as_data_frame() %>%
mutate(zone = paste(lat,long)) %>%
mutate(date = ymd(paste(year, month, 1))) %>%
filter(long < -100) %>%
ggplot(aes(date, surftemp, group = zone, color = lat)) +
geom_line(alpha = 0.1) +
facet_wrap(~cut(lat, breaks = c(-25, 0, 25, 50), ordered_result = T) %>% fct_rev(), ncol=1) +
theme_void()
nasa_long <- nasa %>%
as_data_frame() %>%
mutate(zone = paste(lat,long)) %>%
mutate(dates = ymd(paste(year, month, 1))) %>%
select(dates, zone, surftemp, lat, long) %>%
filter(long < -100) %>%
add_cyclic_data()
b <- nasa_long %>%
filter(id < 100)
ggplot(aes(x_pos, surftemp, alpha = -distance, color = days_ago)) +
geom_vline(data = att_longer %>% filter(month(dates) == 1, day(dates) == 1, days_ago < 364),
aes(xintercept = x_pos, alpha = -distance), color = "gray60", size = 0.4, lty = "dotted") +
geom_segment(data = att_longer %>% filter(day(dates) == 1),
aes(y = 50, yend = 100, xend = x_pos)) +#,
# color = "gray50", size = 0.5) +
# For some reason the month labels seemed to be off by one...
# Might have been time zone or something for day = 0
geom_text(data = att_longer %>% filter(day(dates) == 15, days_ago < 364, distance < -0.2),
aes(y = 0, label = month(dates, label = T, abbr = T), alpha = floor(-distance + 0.3)),
size = 4, color = "gray60", hjust = 0.5, vjust = 0) +
geom_path(aes(size = -days_ago)) +
scale_size(range = c(0.1,1)) +
scale_color_gradient2(low = "yellow", mid = "slateblue", midpoint = 364, high = "darkred") +
# scale_color_viridis_c(direction = -1, option = "A") +
transition_manual(id) +
theme_void() + theme(plot.background = element_rect(fill="black")) +
guides(size = "none", alpha = "none", color = "none")
animate(b, nframes = 5, duration = 10, width = 400, height = 250) #, renderer = av_renderer())
```