Skip to content

Instantly share code, notes, and snippets.

@16EAGLE
Last active November 30, 2019 22:07
Embed
What would you like to do?
Short moveVis example on how to assign multiple path colours per individual to indicate e.g. behavioral segments
# use moveVis >= 0.10.1 for this example
library(moveVis)
library(move)
data("move_data")
# align movement to unique times and regular resolution
m.aligned <- align_move(move_data, res = 4, unit = "mins")
m.split <- split(m.aligned)
# let's use the first individual and colour it by behavioral segment
ts.m <- timestamps(m.split[[1]])
# define behavioral change points and assoicate the segment with colours
cp.m <- list(green = min(ts.m),
red = as.POSIXct("2018-05-15 12:00:00", tz = "UTC"),
blue = as.POSIXct("2018-05-15 14:00:00", tz = "UTC"),
orange = as.POSIXct("2018-05-15 16:00:00", tz = "UTC"))
# iterate through change points and assign colours for segments between change points
col.m <- unlist(mapply(x = cp.m, y = c(cp.m[2:length(cp.m)], max(ts.m)), col = names(cp.m), function(x, y, col){
rep(col, length(which(ts.m >= x & ts.m < y)))
}, SIMPLIFY = F, USE.NAMES = F))
# add colour for the last timestamp to match lengths
col.m <- c(col.m, tail(col.m, n = 1))
# assign colour column, which is recoginced by moveVis
m.split[[1]]$colour <- col.m
# for the other individual, let's use static colours wihout changes
m.split[[2]]$colour <- rep("orange", nrow(m.split[[2]]))
m.split[[3]]$colour <- rep("purple", nrow(m.split[[3]]))
# stack the move split back together
m.stack <- moveStack(m.split)
# create frames and disable path legend
# legend would assoicate initial path colours with individual names: makes no sense, since colours for individual 1 are changing over time
frames <- frames_spatial(m.stack, map_service = "osm", map_type = "terrain_bg", path_legend = F)
# check frame 80 to see, how individual 1 transits in colour
frames[[80]]
# animate frames
animate_frames(frames, out_file = "animation.mov", overwrite = T)
@16EAGLE
Copy link
Author

16EAGLE commented May 6, 2019

result

Copy link

ghost commented Oct 12, 2019

this is a fantastic feature. how would I colour my trace_colour the same as my unique path_colour? it would be nice to have the trace stay behind, but in the path colour rather than the default white trace colour. I'm struggling with how to apply the path_colour to the trace. Thanks!

@popovs
Copy link

popovs commented Nov 30, 2019

Any chance it would be possible to post a simpler example? I'm finding

# iterate through change points and assign colours for segments between change points
col.m <- unlist(mapply(x = cp.m, y = c(cp.m[2:length(cp.m)], max(ts.m)), col = names(cp.m), function(x, y, col){
  rep(col, length(which(ts.m >= x & ts.m < y)))
}, SIMPLIFY = F, USE.NAMES = F))

to be pretty difficult to interpret.

I'm hoping to do something very simple - color any tracks that go north of 65.7 latitude to turn red but have no idea how to subset/work with MoveStack objects...

Thanks! 👍

EDIT:
I've got this solution, for anyone visiting in the future. ani is my MoveStack object:

latitudes <- as.data.frame(ani$y)
names(latitudes) <- "y"
latitudes$colour <- "indianred"
latitudes[latitudes$y >= 65.7,]$colour <- "dodgerblue"
cols <- latitudes$colour # pull out the colors into a list

ani$colour <- cols # now add the "cols" to the "colours" column/feature/not sure what this is of "ani"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment