library(dygraphs, warn.conflicts = FALSE, quietly=TRUE)
library(dplyr, warn.conflicts = FALSE, quietly=TRUE)
library(xts, warn.conflicts = FALSE, quietly=TRUE)
library(lubridate, warn.conflicts = FALSE, quietly=TRUE)
library(stringr)
library(googlesheets4)
library(RColorBrewer)
stepsdata <- read_sheet('https://docs.google.com/spreadsheets/d/1qaBkCycudmIQbMYKbODvaOQ-SmLPCYNRcIVgJHPNo1c/edit?usp=sharing', skip = 1)
## ! Using an auto-discovered, cached token.
## To suppress this message, modify your code or options to clearly consent to
## the use of a cached token.
## See gargle's "Non-interactive auth" vignette for more details:
## <https://gargle.r-lib.org/articles/non-interactive-auth.html>
## ℹ The googlesheets4 package is using a cached token for
## 'garcilin@oregonstate.edu'.
## Auto-refreshing stale OAuth token.
## ✔ Reading from "Samsung Steps Trend".
## ✔ Range '2:10000000'.
# Filtered data set to only contain one observation for each day in 2023.
# The observations kept are those that match the step count in the Samsung
# Health app.
# Columns selected to keep were create_time, count, and speed.
filt_steps <- stepsdata %>%
filter(str_detect(create_time, "^2023")) %>%
select(-1, -2, -4, -10:-13) %>%
filter(source_type == -2) %>%
slice(-c(1)) %>%
select(-2, -5, -6)
head(filt_steps)
## # A tibble: 6 × 3
## create_time count speed
## <dttm> <dbl> <dbl>
## 1 2023-01-01 16:29:11 2057 1.36
## 2 2023-01-02 15:37:16 8059 1.34
## 3 2023-01-03 16:10:52 7413 1.93
## 4 2023-01-04 08:17:44 4827 1.39
## 5 2023-01-05 15:25:44 5043 2.32
## 6 2023-01-06 09:44:35 4683 1.42
# Extensible time-series. Maximize format info preservation for customization.
xts_steps <- xts(x = filt_steps, order.by = as.Date(filt_steps$create_time))
# Make animated time-series plot.
draft_steps <- dygraph(xts_steps, main = "Number of Steps taken in 2023") %>%
dySeries(axis = "y2", "speed") %>%
dySeries("count", strokePattern = "dashed") %>%
dyAxis("y", label = "Number of Steps") %>%
dyAxis("y2", label = "Walking Speed (mph)") %>%
dyOptions(axisLineWidth = 1.5, drawGrid = FALSE,
includeZero = TRUE,
colors = RColorBrewer::brewer.pal(2, "Dark2")) %>%
dyLegend(show = "follow", hideOnMouseOut = FALSE, width = 400)
## Warning in RColorBrewer::brewer.pal(2, "Dark2"): minimal value for n is 3, returning requested palette with 3 different levels
draft_steps