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")
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")
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)))