Set Up
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.2.3
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(timetk)
## Warning: package 'timetk' was built under R version 4.2.3
timetk::plot_time_series
## function (.data, .date_var, .value, .color_var = NULL, .facet_vars = NULL,
## .facet_ncol = 1, .facet_nrow = 1, .facet_scales = "free_y",
## .facet_dir = "h", .facet_collapse = FALSE, .facet_collapse_sep = " ",
## .facet_strip_remove = FALSE, .line_color = "#2c3e50", .line_size = 0.5,
## .line_type = 1, .line_alpha = 1, .y_intercept = NULL, .y_intercept_color = "#2c3e50",
## .x_intercept = NULL, .x_intercept_color = "#2c3e50", .smooth = TRUE,
## .smooth_period = "auto", .smooth_message = FALSE, .smooth_span = NULL,
## .smooth_degree = 2, .smooth_color = "#3366FF", .smooth_size = 1,
## .smooth_alpha = 1, .legend_show = TRUE, .title = "Time Series Plot",
## .x_lab = "", .y_lab = "", .color_lab = "Legend", .interactive = TRUE,
## .plotly_slider = FALSE, .trelliscope = FALSE, .trelliscope_params = list())
## {
## date_var_expr <- rlang::enquo(.date_var)
## value_expr <- rlang::enquo(.value)
## color_var_expr <- rlang::enquo(.color_var)
## if (!is.data.frame(.data)) {
## stop(call. = FALSE, "plot_time_series(.data) is not a data-frame or tibble. Please supply a data.frame or tibble.")
## }
## if (rlang::quo_is_missing(date_var_expr)) {
## stop(call. = FALSE, "plot_time_series(.date_var) is missing. Please supply a date or date-time column.")
## }
## if (rlang::quo_is_missing(value_expr)) {
## stop(call. = FALSE, "plot_time_series(.value) is missing. Please a numeric column.")
## }
## UseMethod("plot_time_series", .data)
## }
## <bytecode: 0x000001c62970a218>
## <environment: namespace:timetk>
Plotting Time Series
taylor_30_min
## # A tibble: 4,032 × 2
## date value
## <dttm> <dbl>
## 1 2000-06-05 00:00:00 22262
## 2 2000-06-05 00:30:00 21756
## 3 2000-06-05 01:00:00 22247
## 4 2000-06-05 01:30:00 22759
## 5 2000-06-05 02:00:00 22549
## 6 2000-06-05 02:30:00 22313
## 7 2000-06-05 03:00:00 22128
## 8 2000-06-05 03:30:00 21860
## 9 2000-06-05 04:00:00 21751
## 10 2000-06-05 04:30:00 21336
## # ℹ 4,022 more rows
taylor_30_min %>%
plot_time_series(.date_var = date, .value = value)
Plotting Groups
m4_daily %>% count(id)
## # A tibble: 4 × 2
## id n
## <fct> <int>
## 1 D10 674
## 2 D160 4197
## 3 D410 676
## 4 D500 4196
m4_daily %>%
group_by(id) %>%
plot_time_series(.date_var = date,
.value = value,
.facet_ncol = 2,
.facet_scales = "free",
.interactive = FALSE)

Plot Visualization and Customization
taylor_30_min %>%
plot_time_series(date, value, .color_var = month(date, label = TRUE),
# Return Static ggplot
.interactive = FALSE,
# Customizing
.title = "Taylor's MegaWatt Data",
.x_lab = "Date (30 Minute Intervals)",
.y_lab = "Energy Demand (MW)",
.color_lab = "Month")

Box Plots
m4_monthly %>% count(id)
## # A tibble: 4 × 2
## id n
## <fct> <int>
## 1 M1 469
## 2 M2 469
## 3 M750 306
## 4 M1000 330
m4_monthly %>%
filter_by_time(.date_var = date, .end_date = "1976") %>%
group_by(id) %>%
plot_time_series_boxplot(.date_var = date,
.value = value,
.period = "1 year",
.facet_ncol = 2)
Regression Plots
m4_monthly %>%
group_by(id) %>%
plot_time_series_regression(.date_var = date,
.facet_ncol = 2,
.formula = log(value) ~ as.numeric(date) + month(date, label = TRUE),
.show_summary = FALSE)