我正在尝试为在 gganimate 中的地图上移动的点设置动画。在下面的示例中,仅对点进行动画处理,点和地图的静态图有效,但将它们组合起来失败并出现错误Error in mapply(FUN = f, ..., SIMPLIFY = FALSE) : zero-length inputs cannot be mixed with those of non-zero length
这是一个复制品:
加载库
# gganimate isn't on CRAN
devtools::install_github('thomasp85/gganimate')
library(tidyverse)
library(gganimate)
library(sf)
# for the spatial data
library(rnaturalearth)
创建数据
# 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)
动画点(作品)
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)
绘制静态地图(作品)
ggplot(d, aes(lon, lat)) +
geom_sf(data = area, inherit.aes = FALSE) +
geom_point()
在背景中使用静态地图为点设置动画(失败)
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)