Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # within each group repeat each point
- # then slice the first and last out and
- # add a variable called linegroup, which provides grouping for start and endpoints of each line
- troops %<>% 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
- # create linestring sf object by summarizing the points,
- # grab the last survivor and direction value of each group (i.e. the 'endpoint' value)
- troops_line <- st_as_sf(troops, coords = c("long", "lat"), crs = 4326) %>%
- group_by(group, linegroup) %>%
- summarise(survivors = last(survivors), direction = last(direction), do_union = FALSE) %>%
- st_cast("LINESTRING")
- gp <- ggplot(troops_line) +
- geom_sf(aes(color = direction, size = survivors), show.legend = "line")
Add Comment
Please, Sign In to add comment