Guest User

Untitled

a guest
Feb 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. # within each group repeat each point
  2. # then slice the first and last out and
  3. # add a variable called linegroup, which provides grouping for start and endpoints of each line
  4. troops %<>% group_by(group) %>%
  5. slice(rep(1:n(), each = 2)) %>%
  6. slice(-c(1, n())) %>%
  7. mutate(linegroup = lapply(1:(n()/2), function(x) rep(x, 2)) %>% unlist) %>%
  8. ungroup
  9.  
  10. # create linestring sf object by summarizing the points,
  11. # grab the last survivor and direction value of each group (i.e. the 'endpoint' value)
  12. troops_line <- st_as_sf(troops, coords = c("long", "lat"), crs = 4326) %>%
  13. group_by(group, linegroup) %>%
  14. summarise(survivors = last(survivors), direction = last(direction), do_union = FALSE) %>%
  15. st_cast("LINESTRING")
  16.  
  17. gp <- ggplot(troops_line) +
  18. geom_sf(aes(color = direction, size = survivors), show.legend = "line")
Add Comment
Please, Sign In to add comment