I'm looking to plot geospatial data in R.
I have a data frame that has Lon, lat and activity as columns. Simplified, it looks something like
structure(list(lon = c(-0.198418, -0.19847, -0.198493, -0.198261,
-0.198308, -0.19826), lat = c(51.565559, 51.565564, 51.565588,
51.565695, 51.565524, 51.565492), ride = c(10L, 10L, 10L, 11L,
11L, 11L)), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), groups = structure(list(ride = 10:11, .rows = list(
1:3, 4:6)), row.names = c(NA, -2L), class = c("tbl_df", "tbl",
"data.frame"), .drop = TRUE))
so something like this, but with several thousand rows per ride:
lon, lat, ride
1 -0.19842 51.566 10
2 -0.19847 51.566 10
3 -0.19849 51.566 10
4 -0.19826 51.566 11
5 -0.19831 51.566 11
6 -0.19826 51.565 11
I want to plot the coordinates into routes on a map based on each ride. I have tried using {leaflet}, with the following code
leaflet(data = unnested_rides) %>%
addTiles() %>%
addPolylines(~lon, ~lat)
but it has no way of distinguishing between the grouping variable meaning it is just one long line, and when I include group it crashes RStudio. If possible I'd like to do this in {leaflet} as it doesn't require a Google API, but would be open to using {ggmaps} if it was significantly easier / better quality.
My intuition is to iterate over the data, adding the grouped data in layers, although I'm not really sure how to go about this. Does anyone have any suggestions? The data has been downloaded from Strava, if that's of any interest to anyone.
Thanks!