R code that generates charts from Strava API (http://strava.github.io/api/v3/activities/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rjson) | |
library(httr) | |
library(ggplot2) | |
library(scales) | |
if (interactive()) { | |
token <- readline(prompt="Enter Strava access token: ") | |
activities <- GET("https://www.strava.com/", path = "api/v3/activities", | |
query = list(access_token = token, per_page = 200)) | |
activities <- content(activities, "text") | |
activities <- fromJSON(activities) | |
activities <- lapply(activities, function(x) { | |
x[sapply(x, is.null)] <- NA | |
unlist(x) | |
}) | |
df <- data.frame(do.call("rbind", activities)) | |
df$distance <- as.numeric(as.character(df$distance)) / 1000 # m -> km | |
df$moving_time <- as.numeric(as.character(df$moving_time)) / 60 # s -> m | |
df$start_date <- as.Date(df$start_date) # yyyy-mm-dd | |
df$average_speed <- as.numeric(as.character(df$average_speed)) * 3.6 # m/s -> km/h | |
ggplot(df, aes(x=distance, y=moving_time)) + | |
geom_point(size=1, aes(colour = type)) + | |
geom_smooth(method=lm, aes(colour = type)) + | |
xlab("distance per activity (kilometers)") + | |
ylab("moving time (minutes)") | |
ggsave("/tmp/moving-time.png") | |
ggplot(df, aes(x=start_date, y=average_speed)) + | |
geom_point(size=1, aes(colour = type)) + | |
scale_x_date(labels = date_format("%Y-%m-%d")) + | |
xlab("date") + | |
ylab("average speed (kilometers per hour)") | |
ggsave("/tmp/speed.png") | |
} else | |
writeLines("Non interactive session, giving up") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment