Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with manually added colours #104

Open
thomadam opened this issue Nov 15, 2021 · 3 comments
Open

Issue with manually added colours #104

thomadam opened this issue Nov 15, 2021 · 3 comments

Comments

@thomadam
Copy link

I am attempting to create an animation of animal movement from a CSV file.

The file is brought into R Studio, converted to a dataframe, and duplicate data is removed.
The data is then divided into the three animal types and sex and each of the converted to a move class object using df2move().
The move object is then assigned a colour using something like AnimalTypeSex$colour = "pink".
Move objects are then converted to a moveStack using moveStack() and the times are aligned using align_move().

The issue arises when using frames_spatial() and this error pops up:
Error in $<-.data.frame(*tmp*, "tail_colour", value = c("#FFD2D2", :
replacement has 99 rows, data has 100

carFS = frames_spatial(AnimalsAligned, map_service = "osm", map_type = "terrain_bg",
trace_show = FALSE,path_fade = F, path_legend = F, path_colours = NA)%>%
add_labels(x="Longitude", y="Latitude")%>%
add_timestamps(AnimalsAligned, type="label")%>%
add_progress()

Is there a way to deal with the replacement has 99 rows, data has 100 issue?

There is no issue if the colour attribute is not used, but then all tracks are randomly coloured.

@swforrest
Copy link

I am also having this issue! Did you happen to find a solution?

@thomadam
Copy link
Author

thomadam commented May 10, 2022 via email

@swforrest
Copy link

I have found another hacky solution, but which might be a bit more straightforward to implement:
For my dataset, aligning the movement introduced NAs into the colour column, which I think would be causing the error.

I then used the 'na.locf' function from the 'zoo' package, which replaces NAs with the previous value appearing in the vector, and fills forward. This obviously assumes that the behavioural state at the NA time step is the same as the previous time step (or earlier if multiple consecutive NAs), but for something like sex then this would be fine.

I used this after the align move step:

# animating simluated data from HMM
sim1.move <- df2move(df = sim1, proj = "EPSG:4326", x = 'x', y = 'y', time = 'time.stamps', track_id = "ID")

# add the colour vector back into the data frame
sim1.move$colour <- sim1$colour 

# align_move to get move object, which keeps the colour vector but introduces NAs
move.obj <- align_move(sim1.move, res = 1, unit = "hours")

# fill NAs with the colour value that appeared previously in the vector
move.obj$colour <- na.locf(move.obj$colour)

# you can also use the argument 'fromLast = TRUE' to fill from the next value rather than the previous
# move.obj$colour <- na.locf(move.obj$colour, fromLast = TRUE)

# frames_spatial now works fine, and the colour is according to behavioural state in my example
frames <- frames_spatial(move.obj, 
                         alpha = 0, # for white background (my simulated data had no reference point)
                         path_legend = F,
                         equidistant = T,
                         trace_show = T)

Then the animate_frames also appears to work fine!
simulatedHMM6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants