Skip to content

Commit

Permalink
fixing get_heat_map for streams and changing readme examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fawda123 committed Mar 26, 2024
1 parent 6a98869 commit ef70a73
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
19 changes: 8 additions & 11 deletions R/get_heat_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @param acts numeric indicating which activities to plot based on index in the activities list, defaults to most recent
#' @param id optional numeric vector to specify the id(s) of the activity/activities to plot, \code{acts} is ignored if provided
#' @param alpha the opacity of the line desired. A single activity should be 1. Defaults to 0.5
#' @param add_elev logical indicating if elevation is overlayed by color shading on the activity lines
#' @param add_elev logical indicating if elevation is shown by color shading on the activity lines
#' @param as_grad logical indicating if elevation is plotted as percent gradient, applies only if \code{add_elev = TRUE}
#' @param filltype chr string specifying which stream variable to use for filling line segments, applies only to \code{strframe} objects, acceptable values are \code{"elevation"}, \code{"distance"}, \code{"slope"}, or \code{"speed"}
#' @param distlab logical if distance labels are plotted along the route
Expand Down Expand Up @@ -185,10 +185,7 @@ get_heat_map.actframe <- function(act_data, key, alpha = NULL, add_elev = FALSE,
dplyr::ungroup(.) %>%
dplyr::select(-tosel, -diffdist) %>%
dplyr::mutate(distance = as.character(round(distance)))
# final <- temp[nrow(temp), ]
# final$distance <- format(final$distance, nsmall = 1, digits = 1)
# disttemp <- rbind(disttemp, final)


# add to plot
p <- p +
ggspatial::geom_spatial_label_repel(
Expand Down Expand Up @@ -252,7 +249,7 @@ get_heat_map.strframe <- function(act_data, alpha = NULL, filltype = 'elevation'
alti <- stats::approx(x= x$altitude, n = expand * nrow(x))$y
grds <- stats::approx(x= x$grade_smooth, n = expand * nrow(x))$y
vels <- stats::approx(x= x$velocity_smooth, n = expand * nrow(x))$y
data.frame(id = unique(x$id), lat = yint, lng = xint, distance = dist, elevation = alti, slope = grds, speed = vels)
data.frame(id = unique(x$id), lat = yint, lon = xint, distance = dist, elevation = alti, slope = grds, speed = vels)

})
temp <- do.call('rbind', temp)
Expand All @@ -270,7 +267,7 @@ get_heat_map.strframe <- function(act_data, alpha = NULL, filltype = 'elevation'
if(filltype == 'slope') leglab <- '%'
else leglab <- unit_vals[filltype]
p <- pbase +
ggspatial::geom_spatial_path(ggplot2::aes_string(x = 'lng', y = 'lat', group = 'id', colour = filltype),
ggspatial::geom_spatial_path(ggplot2::aes_string(x = 'lon', y = 'lat', group = 'id', colour = filltype),
alpha = alpha, data = temp, linewidth = size, crs = 4326) +
ggplot2::scale_colour_distiller(leglab, palette = col)

Expand All @@ -288,21 +285,21 @@ get_heat_map.strframe <- function(act_data, alpha = NULL, filltype = 'elevation'
dplyr::ungroup(.) %>%
dplyr::select(-tosel, -diffdist) %>%
dplyr::mutate(distance = as.character(round(distance)))
# final <- temp[nrow(temp), ]
# final$distance <- format(final$distance, nsmall = 1, digits = 1)
# disttemp <- rbind(disttemp, final)

# add to plot
p <- p +
ggspatial::geom_spatial_label_repel(
data = disttemp,
ggplot2::aes(x = lng, y = lat, label = distance),
ggplot2::aes(x = lon, y = lat, label = distance),
point.padding = grid::unit(0.4, "lines"),
crs = 4326
)

}

p <- p +
ggplot2::coord_sf(xlim = range(temp$lon), ylim = range(temp$lat))

return(p)

}
39 changes: 20 additions & 19 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -109,49 +109,50 @@ An example creating a heat map of activities:
```{r message = F, fig.height = 5.5, fig.width = 7, message = F, warning = F}
library(dplyr)
# get activities, get activities by lat/lon, distance, plot
my_acts <- get_activity_list(stoken)
act_data <- compile_activities(my_acts) %>%
filter(start_latlng2 < -86.5 & start_latlng2 > -88.5) %>%
filter(start_latlng1 < 31.5 & start_latlng1 > 30) %>%
filter(distance > 20)
get_heat_map(act_data, key = google_key, col = 'darkgreen', size = 2, distlab = F, f = 0.4)
# get activities by date range
my_acts <- get_activity_list(stoken, after = as.Date('2020-12-31'))
act_data <- compile_activities(my_acts)
# subset by location
toplo <- act_data %>%
filter(grepl('Run$', name)) %>%
filter(start_latlng2 < -82.63 & start_latlng2 > -82.65) %>%
filter(start_latlng1 < 27.81 & start_latlng1 > 27.78)
get_heat_map(toplo, key = google_key, col = 'darkred', size = 1.5, distlab = F, alpha = 0.6, zoom = 13)
```

Plotting elevation and grade for a single ride:
```{r}
# activity id
id <- 1784292574
# get data for a single activity
my_acts <- get_activity_list(stoken, id = 1784292574)
act_data <- compile_activities(my_acts)
# plot elevation along a single ride
get_heat_map(my_acts, id = id, alpha = 1, add_elev = T, distlab = F, key = google_key, size = 2, col = 'Spectral', units = 'imperial')
get_heat_map(my_acts, alpha = 1, add_elev = T, distlab = F, key = google_key, size = 2, col = 'Spectral', units = 'imperial')
# plot % gradient along a single ride
get_heat_map(my_acts, id = id, alpha = 1, add_elev = T, distlab = F, as_grad = T, key = google_key, size = 2, col = 'Spectral', expand = 5, units = 'imperial')
get_heat_map(my_acts, alpha = 1, add_elev = T, distlab = F, as_grad = T, key = google_key, size = 2, col = 'Spectral', units = 'imperial')
```

Get elevation profiles for activities:
```{r message = F, fig.height = 3, fig.width = 9}
# get activities
my_acts <- get_activity_list(stoken)
get_elev_prof(my_acts, id = id, key = google_key, units = 'imperial')
get_elev_prof(my_acts, id = id, key = google_key, units = 'imperial', total = T)
get_elev_prof(my_acts, key = google_key, units = 'imperial')
get_elev_prof(my_acts, key = google_key, units = 'imperial', total = T)
```

Plot average speed per split (km or mile) for an activity:
```{r message = F, fig.height = 3, fig.width = 9}
# plots for most recent activity
plot_spdsplits(my_acts, stoken, id = id, units = 'imperial')
plot_spdsplits(my_acts, stoken, units = 'imperial')
```

Additional functions are provided to get "stream" information for individual activities. Streams provide more detailed information about location, time, speed, elevation, gradient, cadence, watts, temperature, and moving status (yes/no) for an individual activity.

Use `get_activity_streams` for detailed info about activities:
```{r, fig.height = 4, fig.with = 4}
# get streams for the first activity in my_acts
strms_data <- get_activity_streams(my_acts, stoken, id = id)
strms_data <- get_activity_streams(my_acts, stoken)
head(strms_data)
```

Expand Down
Binary file modified man/figures/unnamed-chunk-12-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-13-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-13-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion man/get_heat_map.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ef70a73

Please sign in to comment.