我正在尝试以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)
答案 0 :(得分:2)
data = d
和aes()
从ggplot()
移至geom_point()
transition_components()
更改为transition_time()
shadow_trail
更改为shadow_wake
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)
答案 1 :(得分:0)
trackid 是不确定的,在p2中引发错误; 时间已定义。