79246103

Date: 2024-12-03 02:24:34
Score: 0.5
Natty:
Report link

Another option is to keep using geom_path to plot the lines, but then to use coord_sf to apply a spatial coordinate system to ggplot:

ggplot() +
geom_path(data = df, 
          aes(lon, lat, color = type, group = 1)) + 
coord_sf(crs = "EPSG:4326")

Plot using geom_path and coord_sf

If you want to project it into a different CRS, use st_transform to project the coordinates, then pull the projected coordinates and plot them using geom_path again.

df_projected <- 
  st_as_sf(df, coords = c("lon", "lat")) |>
  st_set_crs("EPSG:4326") |>
  st_transform("EPSG:32624") |>
  mutate(easting = st_coordinates(geometry)[, "X"],
         northing = st_coordinates(geometry)[, "Y"])

ggplot() +
  geom_path(data = df_projected, 
            aes(easting, northing, color = type, group = 1)) + 
  coord_sf(crs = "EPSG:32624")

Plot using geom_path and coord_sf with reprojected coordinates

Data:

df <- tibble(time = seq(1,21),
             lon = seq(-50,-30, 1) + {set.seed(123); rnorm(n = 21)},
             lat = seq(10, 20, 0.5) + {set.seed(456); rnorm(n = 21)},
             type = c(rep('A',5),rep('B',10), rep('A',6)))
Reasons:
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Skyler Lewis