Skip to content

Instantly share code, notes, and snippets.

@thomasp85
Last active December 9, 2020 18:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomasp85/0b7388f6d505ecfda623cd3d740d81f2 to your computer and use it in GitHub Desktop.
Save thomasp85/0b7388f6d505ecfda623cd3d740d81f2 to your computer and use it in GitHub Desktop.
Animating Elephant Seal movement
library(sf)
library(ggplot2)
library(gganimate)
# Data from personal correspondance
# Collapse all dates to the same year
d$year <- format(d$date, '%Y')
d$stand_time <- as.POSIXct(paste0('2000-', format(d$date, '%m-%d %T')))
# Create topo background
earth <- st_as_sf(rnaturalearth::countries110)
lon_range <- range(d$lon) + c(-20, 20)
lat_range <- range(d$lat) + c(-20, 20)
bbox <- st_polygon(list(cbind(lon_range[c(1,1,2,2,1)], lat_range[c(1,2,2,1,1)])))
bbox <- st_sfc(bbox)
st_crs(bbox) <- st_crs(earth)
area <- st_intersection(earth, bbox)
# Plot and animate
p <- ggplot(d) +
geom_sf(data = area, fill = 'white') +
geom_point(aes(lon, lat, group = id, colour = year), size = 2.5) +
coord_sf(xlim = range(d$lon), ylim = range(d$lat)) +
labs(title = 'Tracking of southern elephant seals over the year',
subtitle = 'Date: {format(frame_time, "%b %e")}',
x = NULL, y = NULL) +
theme(panel.background = element_rect(fill = 'lightblue'),
legend.position = 'bottom') +
transition_components(stand_time) +
shadow_trail(distance = 0.01, size = 0.3)
animate(p, 200, 10)
@MiaWege
Copy link

MiaWege commented Jul 4, 2018

Hi Thomas,
I have tried running this code on my own tracks, but I keep getting the error "Error in transition_components(id, stand_time) :
could not find function "transition_components"" I have all the relevant packages loaded (sf, ggplot2 and gganimate), I have the latest R (R-3.5.1) installed and gganimate version 0.1.1 (which I installed using devtools::install_github("dgrtwo/gganimate")) It seems it should be a package version problem or something? I see you have created your own version of gganimate, is the function transition-components in there perhaps? And how do I then download your version of gganimate?

@kanishkamisra
Copy link

Hi @Miasjien! You can re-install the gganimate package by doing:

devtools::install_github('thomasp85/gganimate')

@Maschette
Copy link

Hi Thomas,
I have a subset of this data (it is in the SOmap package) and when I try and repeat this I get the error:
animate(p, 200, 10)
Error in mapply(FUN = f, ..., SIMPLIFY = FALSE) :
zero-length inputs cannot be mixed with those of non-zero length

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