I have two spatial features:
library(sf)
points1 <- data.frame(foo = seq(15, 75, 15),
long = c(-85, -80, -78, -75, -82),
lat = c(34, 36, 37, 38, 35)) %>%
st_as_sf(coords = c('long', 'lat'), crs = 4326)
points2 <- data.frame(bar = seq(15, 75, 15),
long = c(85, 80, 78, 75, 82),
lat = c(30, 32, 34, 36, 38)) %>%
st_as_sf(coords = c('long', 'lat'), crs = 4326)
cbind(points1, points2) -> df
This gives:
foo bar geometry geometry.1
1 15 15 POINT (-85 34) POINT (85 30)
2 30 30 POINT (-80 36) POINT (80 32)
3 45 45 POINT (-78 37) POINT (78 34)
4 60 60 POINT (-75 38) POINT (75 36)
5 75 75 POINT (-82 35) POINT (82 38)
I'd like to draw a line between pairs of points within df
- so from a POINT in geometry
to a POINT in geometry.1
. I have tried to cast the POINTs to a LINESTRING as follows:
df %>% summarise(do_union=F) %>% st_cast("LINESTRING") %>% plot()
, but this doesn't seem to work. I get a continuous line, when what I want is five separate lines.