-
Notifications
You must be signed in to change notification settings - Fork 3
/
time_series_plots.R
363 lines (332 loc) · 9.99 KB
/
time_series_plots.R
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
#' Time Series Plot
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description
#' This is a warpper function to the [timetk::plot_time_series()] function with
#' a limited functionality parameter set. To see the full reference please visit
#' the `timetk` package site.
#'
#' @details This function takes only a few of the arguments in the function and
#' presets others while choosing the defaults on others. The smoother functionality
#' is turned off.
#'
#' @param .data The data to pass to the function, must be a tibble/data.frame.
#' @param .date_col The column holding the date.
#' @param .value_col The column holding the value.
#' @param .color_col The column holding the variable for color.
#' @param .facet_col The column holding the variable for faceting.
#' @param .facet_ncol How many columns do you want.
#' @param .interactive Return a `plotly` plot if set to TRUE and a static `ggplot2`
#' plot if set to FALSE. The default is FALSE.
#'
#' @seealso
#' \url{https://business-science.github.io/timetk/reference/plot_time_series.html}
#'
#' @examples
#' suppressPackageStartupMessages(library(dplyr))
#' library(timetk)
#' library(healthyR.data)
#'
#' healthyR.data::healthyR_data %>%
#' filter(ip_op_flag == "I") %>%
#' select(visit_end_date_time, service_line) %>%
#' filter_by_time(
#' .date_var = visit_end_date_time
#' , .start_date = "2020"
#' ) %>%
#' group_by(service_line) %>%
#' summarize_by_time(
#' .date_var = visit_end_date_time
#' , .by = "month"
#' , visits = n()
#' ) %>%
#' ungroup() %>%
#' ts_plt(
#' .date_col = visit_end_date_time
#' , .value_col = visits
#' , .color_col = service_line
#' )
#'
#' @return
#' A `plotly` plot or a `ggplot2` static plot
#'
#' @export
#'
ts_plt <- function(
.data
, .date_col
, .value_col
, .color_col = NULL
, .facet_col = NULL
, .facet_ncol = NULL
, .interactive = FALSE
) {
# I* Tidyeval ----
date_var_expr <- rlang::enquo(.date_col)
value_var_expr <- rlang::enquo(.value_col)
color_var_expr <- rlang::enquo(.color_col)
facet_var_expr <- rlang::enquo(.facet_col)
facet_ncol_expr <- rlang::enquo(.facet_ncol)
interactive_var_expr <- .interactive
# * Checks ----
if(!is.data.frame(.data)) {
stop(call. = FALSE,"(.data) is not a tibble/data.frame. Please supply")
}
if(rlang::quo_is_missing(date_var_expr)){
stop(call. = FALSE,"(.date_col) is missing. Please supply.")
}
if(rlang::quo_is_missing(value_var_expr)){
stop(call. = FALSE, "(.value_col) is missing. Please supply.")
}
# * Data ----
data_tbl <- tibble::as_tibble(.data)
# * Plot ----
plt <- data_tbl %>%
timetk::plot_time_series(
.date_var = {{ date_var_expr }}
, .value = {{ value_var_expr }}
, .color_var = {{ color_var_expr }}
, .facet_vars = {{ facet_var_expr }}
, .facet_ncol = {{ facet_ncol_expr }}
, .interactive = interactive_var_expr
, .smooth = FALSE
)
# * Return ----
return(plt)
}
#' Plot ALOS - Average Length of Stay
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description
#' Plot ALOS - Average Length of Stay
#'
#' @details
#' - Expects a tibble with a date time column and a value column
#' - Uses `timetk` for underlying sumarization and plot
#' - If .by_grouping is missing it will default to "day"
#' - A static ggplot2 object is return if the .interactive function is FALSE
#' otherwise a `plotly` plot is returned.
#'
#' @param .data The time series data you need to pass
#' @param .date_col The date column
#' @param .value_col The value column
#' @param .by_grouping How you want the data summarized - "sec", "min", "hour",
#' "day", "week", "month", "quarter" or "year"
#' @param .interactive TRUE or FALSE. TRUE returns a `plotly` plot and FALSE
#' returns a static `ggplot2` plot
#'
#' @examples
#' library(healthyR)
#' library(healthyR.data)
#' library(timetk)
#' library(dplyr)
#' library(purrr)
#'
#' # Make A Series of Dates ----
#' data_tbl <- healthyR_data
#'
#' df_tbl <- data_tbl %>%
#' filter(ip_op_flag == "I") %>%
#' select(visit_end_date_time, length_of_stay) %>%
#' summarise_by_time(
#' .date_var = visit_end_date_time
#' , .by = "day"
#' , visits = mean(length_of_stay, na.rm = TRUE)
#' ) %>%
#' filter_by_time(
#' .date_var = visit_end_date_time
#' , .start_date = "2012"
#' , .end_date = "2019"
#' ) %>%
#' set_names("Date","Values")
#'
#' ts_alos_plt(
#' .data = df_tbl
#' , .date_col = Date
#' , .value_col = Values
#' , .by = "month"
#' , .interactive = FALSE
#' )
#'
#' @return
#' A timetk time series plot
#'
#' @export
#'
ts_alos_plt <- function(.data, .date_col, .value_col, .by_grouping, .interactive) {
# * Tidyeval ----
date_var_expr <- rlang::enquo(.date_col)
value_var_expr <- rlang::enquo(.value_col)
by_var_expr <- .by_grouping
interactive_var_expr <- .interactive
# * Checks ----
if(!is.data.frame(.data)) {
stop(call. = FALSE, "(data) is not a data-frame or tibble. Please supply.")
}
if (rlang::quo_is_missing(date_var_expr)) {
stop(call. = FALSE, "(date_var_expr) is missing. Please supply.")
}
if(rlang::quo_is_missing(value_var_expr)) {
stop(call. = FALSE, "(value_var_expr) is missing. Please supply.")
}
# * Manipulate ----
df_tbl <- tibble::as_tibble(.data) %>%
dplyr::select( {{date_var_expr}}, {{value_var_expr}} ) %>%
purrr::set_names("date", "value")
# If .by is missing then manipulate
if(by_var_expr == "") {
df_summarised_tbl <- timetk::summarise_by_time(
.data = df_tbl
, .date_var = date
, value = mean(value)
)
} else {
df_summarised_tbl <- timetk::summarise_by_time(
.data = df_tbl
, .date_var = date
, .by = by_var_expr
, value = mean(value)
)
}
# * Plot ----
if(!interactive_var_expr) {
g <- df_summarised_tbl %>%
timetk::plot_time_series(
.date_var = date
, .value = value
, .title = "Average Length of Stay Plot"
, .interactive = FALSE
) +
ggplot2::theme_minimal()
} else {
g <- df_summarised_tbl %>%
timetk::plot_time_series(
.date_var = date
, .value = value
, .title = "Average Length of Stay Plot"
, .interactive = FALSE
) +
ggplot2::theme_minimal()
g <- plotly::ggplotly(g)
}
# * Return ----
return(g)
}
#' Plot Readmit Rate
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description
#' Plot Readmit Rate
#'
#' @details
#' - Expects a tibble with a date time column and a value column
#' - Uses `timetk` for underlying sumarization and plot
#' - If .by_grouping is missing it will default to "day"
#'
#' @param .data The data you need to pass.
#' @param .date_col The date column.
#' @param .value_col The value column.
#' @param .by_grouping How you want the data summarized - "sec", "min", "hour",
#' "day", "week", "month", "quarter" or "year".
#' @param .interactive TRUE or FALSE. TRUE returns a `plotly` plot and FALSE
#' returns a static `ggplot2` plot.
#'
#' @examples
#' set.seed(123)
#'
#' suppressPackageStartupMessages(library(timetk))
#' suppressPackageStartupMessages(library(purrr))
#' suppressPackageStartupMessages(library(dplyr))
#'
#' ts_tbl <- tk_make_timeseries(
#' start = "2019-01-01"
#' , by = "day"
#' , length_out = "1 year 6 months"
#' )
#' values <- arima.sim(
#' model = list(
#' order = c(0, 1, 0))
#' , n = 547
#' , mean = 1
#' , sd = 5
#' )
#'
#' df_tbl <- tibble(
#' x = ts_tbl
#' , y = values
#' ) %>%
#' set_names("Date","Values")
#'
#' ts_readmit_rate_plt(
#' .data = df_tbl
#' , .date_col = Date
#' , .value_col = Values
#' , .by = "month"
#' , .interactive = FALSE
#' )
#'
#' @return
#' A `timetk` time series plot that is interactive
#'
#' @export
#'
ts_readmit_rate_plt <- function(.data, .date_col, .value_col, .by_grouping, .interactive) {
# * Tidyeval ----
date_var_expr <- rlang::enquo(.date_col)
value_var_expr <- rlang::enquo(.value_col)
by_var_expr <- .by_grouping
interactive_var_expr <- .interactive
# * Checks ----
if(!is.data.frame(.data)) {
stop(call. = FALSE, "(data) is not a data-frame or tibble. Please supply.")
}
if (rlang::quo_is_missing(date_var_expr)) {
stop(call. = FALSE, "(date_var_expr) is missing. Please supply.")
}
if(rlang::quo_is_missing(value_var_expr)) {
stop(call. = FALSE, "(value_var_expr) is missing. Please supply.")
}
# * Data ----
df_tbl <- tibble::as_tibble(.data) %>%
dplyr::select( {{date_var_expr}}, {{value_var_expr}} ) %>%
purrr::set_names("date", "value")
# * Manipulate ----
# If .by is missing then manipulate
if(by_var_expr == "") {
df_summarised_tbl <- timetk::summarise_by_time(
.data = df_tbl
, .date_var = date
, value = mean(value)
)
} else {
df_summarised_tbl <- timetk::summarise_by_time(
.data = df_tbl
, .date_var = date
, .by = by_var_expr
, value = mean(value)
)
}
# * Plot ----
if(!interactive_var_expr) {
g <- df_summarised_tbl %>%
timetk::plot_time_series(
.date_var = date
, .value = value
, .title = "Readmission Rate Plot"
, .interactive = FALSE
)
} else {
g <- df_summarised_tbl %>%
timetk::plot_time_series(
.date_var = date
, .value = value
, .title = "Readmission Rate Plot"
, .interactive = TRUE
)
}
# * Return ----
return(g)
}