Corned Beef Hash Map
Corned Beef Hash Map

Reputation: 221

gganimate won't render when combining geom_point and geom_sf

I'm trying to animate points moving over a map in gganimate. In teh following example, animating just the points works and a static plot of the points and map works, but combining them fails with the error Error in mapply(FUN = f, ..., SIMPLIFY = FALSE) : zero-length inputs cannot be mixed with those of non-zero length

Here's a repro:

Load libraries

# gganimate isn't on CRAN
devtools::install_github('thomasp85/gganimate')
library(tidyverse)
library(gganimate)
library(sf)
# for the spatial data
library(rnaturalearth)             

Create data

# Points data
time <- seq(ISOdate(2015, 6, 1), ISOdate(2015, 8, 1), length.out = 100)
track1 <- tibble(lon = seq(-161, -155, length.out = 100),
                 lat = seq(19, 25, length.out = 100),
                 time = time,
                 trackid = 1)
track2 <- tibble(lon = seq(-155, -161, length.out = 100),
                 lat = seq(19, 25, length.out = 100),
                 time = time,
                 trackid = 2)
d <- rbind(track1, track2)

# Spatial data
earth <- st_as_sf(ne_download(scale = "medium",
                              category = "physical",
                              type = "coastline"))
deg_buff <- 1
lon_range <- range(d$lon) + c(-deg_buff, deg_buff)
lat_range <- range(d$lat) + c(-deg_buff, deg_buff)
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)

Animate points (works)

p <- ggplot(d, aes(lon, lat)) +
  geom_point() +
  labs(subtitle = 'Date: {format(frame_time, "%b %e")}') +
  transition_components(trackid, time) +
  shadow_trail(distance = 0.01, size = 0.3)
animate(p, 100, 20)

Plot a static map (works)

ggplot(d, aes(lon, lat)) +
  geom_sf(data = area, inherit.aes = FALSE) +
  geom_point()

Animate points with static map in background (fails)

p2 <- ggplot(d, aes(lon, lat)) +
  geom_sf(data = area, inherit.aes = FALSE) +
  geom_point() +
  labs(subtitle = 'Date: {format(frame_time, "%b %e")}') +
  transition_components(trackid, time) +
  shadow_trail(distance = 0.01, size = 0.3)
animate(p2, 100, 20)

Upvotes: 0

Views: 1649

Answers (2)

Roman
Roman

Reputation: 4989

1

  1. Moved data = d and aes() from ggplot() to geom_point()
  2. Changed transition_components() to transition_time()
  3. Changed shadow_trail to shadow_wake
  4. (Added color)

Code

p2 <- ggplot() +
    geom_sf(data = area, color = "red") +
    geom_point(data = d, aes(lon, lat), inherit.aes = FALSE) +
    labs(subtitle = 'Date: {format(frame_time, "%b %e")}') +
    transition_time(time) +
    shadow_wake(0.3)

animate(p2, 100)

Upvotes: 2

Richard Careaga
Richard Careaga

Reputation: 670

trackid is undefined and what throws the error in p2; time is defined.

Upvotes: 0

Related Questions