/
activatr.Rmd
117 lines (88 loc) · 3.59 KB
/
activatr.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
---
title: "Visualizing Activities with activatr"
author: "Daniel Schafer"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Visualizing Activities with activatr}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
`activatr` (pronounced like the word "activator") is a library for parsing GPX files into a standard format, and then manipulating and visualizing those files.
## Getting GPX Files
The process to get a GPX file varies depending on the service you use. In Garmin Connect, you can click the gear menu on an activity and click "Export to GPX". This package includes sample GPXs as examples.
## Parsing GPX Files
Basic parsing of a GPX file is simple: we use the `parse_gpx()` function and pass it the name of the GPX file.
```{r filename}
library(activatr)
# Get the running_example.gpx file included with this package.
filename <- system.file(
"extdata",
"running_example.gpx.gz",
package = "activatr"
)
df <- parse_gpx(filename)
```
`parse_gpx()` returns an `act_tbl`, which has a column for latitude (`lat`), longitude (`lon`), elevation (`ele`, in meters), and time (`time`).
```{r table, echo=FALSE, results='asis'}
knitr::kable(head(df, 5))
```
`activatr` also overrides `summary()` to create a basic one-row tibble summarizing the activity.
```{r summary, results = "hide"}
summary(df)
```
```{r summary_table, echo=FALSE, results='asis'}
knitr::kable(summary(df))
```
For more advanced parsing options, see `vignette("parsing")`.
## Analyzing GPX Files
Since this is just a tibble, we can analyze and plot it using usual techniques and libraries. `activatr` includes a few helpers, like `mutate_with_speed()`, `speed_to_mile_pace()` and `pace_formatter()` to make it easier to analyze pace using these libraries.
```{r pacegraph, message = FALSE, warning = FALSE}
library(ggplot2)
library(dplyr)
df |>
mutate_with_speed(lead = 10, lag = 10) |>
mutate(pace = speed_to_mile_pace(speed)) |>
filter(as.numeric(pace) < 1200) |>
ggplot() +
geom_line(aes(x = time, y = as.numeric(pace)), color = "blue") +
scale_y_reverse(label = pace_formatter) +
xlab("Time") +
ylab("Pace (min/mile)")
```
For more details on those helpers, see `vignette("pace")`.
## Visualizing GPX Files
Once we have the data, it's useful to visualize it. While basic visualizations work as expected with a data frame:
```{r basicplot, warning = FALSE}
library(ggplot2)
qplot(lon, lat, data = df)
```
It's more helpful to overlay this information on a map. To aid in that, `get_ggmap_from_df()` is a wrapper around `ggmap::get_map()` that returns a correctly sized and zoomed map, atop which we can visualize our track using `ggmap::ggmap()`.
Let's see that on its own to start:
```{r mapplot_display, eval = FALSE}
library(ggmap)
ggmap::ggmap(get_ggmap_from_df(df)) + theme_void()
```
```{r mapplot_run, echo = FALSE}
# running_example_ggmap is the saved result of calling get_ggmap_from_df(df)
# We don't run that here because it requires an API key.
df_ggmap <- running_example_ggmap
ggmap::ggmap(running_example_ggmap) + theme_void()
```
We now have a map at the right size to visualize the run. Putting it
all together, we can make a nice basic graphic of the run:
```{r finalplot_display, eval = FALSE}
ggmap::ggmap(get_ggmap_from_df(df)) +
theme_void() +
geom_path(aes(x = lon, y = lat), linewidth = 1, data = df, color = "red")
```
```{r finalplot_run, echo = FALSE}
ggmap::ggmap(running_example_ggmap) +
theme_void() +
geom_path(aes(x = lon, y = lat), linewidth = 1, data = df, color = "red")
```