Created
August 4, 2017 16:39
-
-
Save joebrew/bbf8d8b7a71518b5392a1fb2179b8142 to your computer and use it in GitHub Desktop.
View Strava data with leaflet
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
# Define function for leaflet plot of only a country | |
leaf_country <- function(location, | |
limit = NULL, | |
city = FALSE){ # or NULL | |
if(city){ | |
sub_data <- df %>% | |
filter(city == location) | |
} else { | |
sub_data <- df %>% | |
filter(country == location) | |
} | |
l <- leaflet() %>% | |
addProviderTiles(provider = 'CartoDB.DarkMatter', | |
options = providerTileOptions(opacity = 0.95)) | |
captured_activities <- sub_data %>% | |
group_by(activity_id) %>% | |
tally %>% | |
.$activity_id | |
if(!is.null(limit)){ | |
if(limit < length(captured_activities)){ | |
captured_activities <- sample(captured_activities, | |
limit) | |
} | |
} | |
the_alpha <- ifelse(length(captured_activities) > 50, | |
0.4, | |
ifelse(length(captured_activities) > 100, | |
0.3, | |
0.5)) | |
for (i in 1:length(captured_activities)){ | |
message(i) | |
this_activity_id <- captured_activities[i] | |
sub_sub_data <- sub_data %>% | |
filter(activity_id == this_activity_id) | |
l <- l %>% | |
addPolylines(data = sub_sub_data, lng = ~lon, lat = ~lat, | |
color = 'red', | |
opacity = 1 - the_alpha, | |
weight = 0.3) | |
} | |
return(l) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment