1

I've got some GPS data (latlong) and I want to plot the GPS points and their connecting lines and color both by the time difference between the two GPS points. I've figured out how to color the points and convert the points to a LINESTRING but I can't figure out how to recolor the scale of the line.

I saw this post: Color portions of sf LINESTRING by variable that shows how to break the linestrings into segments and color the segments by a categorical variable but as I have close to 100,000 observations I'd like to avoid just splitting my plot up into 99,999 pieces and also, my data is continuous.

Here's some toy data:


# Create some data points
fake_data = data.frame(Time = 1:6,
                       Long = c(-90.46200, -90.46160, -90.46170, -90.46150, -90.46100, -90.46240),
                       Lat = c(33.88540, 33.88750, 33.88520, 33.88340, 33.88540, 33.88150))
# Define as points
points = st_as_sf(fake_data, coords = c("Long", "Lat"), crs = 4326, remove = FALSE)
# Connect the dots
lines = points %>% summarize(do_union = FALSE) %>% st_cast("LINESTRING")


library(ggplot2)
# Plot
ggplot(data = points)+
  geom_sf(aes(color = as.numeric(points$Time)))+
  geom_sf(data = lines)+#, aes(color = numeric(points$Time[1:(length(points$Time)-1)])))+ #did not work
  ylim(c(33.87, 33.89))+
  xlim(c(-90.47, -90.45))+
  scale_color_gradient(name = "Time", position="bottom" , low = "blue", high = "red")

enter image description here

Thank you!

Jade131621
  • 316
  • 1
  • 13

1 Answers1

1

I'm confident there are prettier ways to do this, but this works!

I needed to add in a group variable to use to generate linegroups. This was inspired by: https://stackoverflow.com/a/48979401/3642716 and their answer with how to solve for troops in the tidyverse dataset.

library(sf)
library(dplyr)
library(ggplot2)
# Create some data points
fake_data = data.frame(Time = 1:6,
                       Long = c(-90.46200, -90.46160, -90.46170, -90.46150, -90.46100, -90.46240),
                       Lat = c(33.88540, 33.88750, 33.88520, 33.88340, 33.88540, 33.88150),
                       group = 1)
# Define as points
points = st_as_sf(fake_data, coords = c("Long", "Lat"), crs = 4326, remove = FALSE)
# Connect the dots
lines <- fake_data
lines %<>% group_by(group) %>%
  slice(rep(1:n(), each = 2)) %>%
  slice(-c(1, n())) %>%
  mutate(linegroup = lapply(1:(n()/2), function(x) rep(x, 2)) %>% unlist) %>% 
  ungroup %>%
  group_by(linegroup) %>%
  st_as_sf(coords = c("Long","Lat"), crs = 4326, remove = F) %>%
  summarize( do_union = F) %>%
  st_cast("LINESTRING")


# Plot
ggplot(data = points)+
  geom_sf(aes(color = `Time`))+
  geom_sf(data = lines, aes(color = `linegroup`))+#, aes(color = numeric(points$Time[1:(length(points$Time)-1)])))+ #did not work
  ylim(c(33.881, 33.888))+
  xlim(c(-90.463, -90.460))+
  scale_color_gradient(name = "Time", position="bottom" , low = "blue", high = "red")

Looks like this:

Colourful Line Segments

Badger
  • 1,043
  • 10
  • 25
  • 1
    Thanks for responding! I'm not super familiar with tidyverse. What does the %<>% operator do? – Jade131621 Feb 02 '23 at 18:40
  • Thanks for having a question with a minimally reproducible example! That's the tidy way of doing some work and then assigning it back to the initially called element. I think it started in tidy, but is now a part of magittr and ships with R if I'm not mistaken! Super handy! – Badger Feb 02 '23 at 19:21