Skip to content

Instantly share code, notes, and snippets.

@thomasp85
Last active December 9, 2020 18:23
Show Gist options
  • 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)
@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