我正在尝试使用
gganimate
随着时间的推移对位置进行动画处理,并拥有一个从 2004 年到 2022 年运行的数据集。不幸的是,我没有 2020 年的数据(由于新冠疫情)。当我尝试使用 gganimate
时,2020 年会在动画中显示,并带有一堆 datapoints
,尽管 2020 年不在我的 df 中的任何位置。如何绘制 2004-2022 年而不包括 2020 年?
我尝试过使用
nframes
和 exit_disappear(early=TRUE)
但无济于事。
代码如下:
with_data <- basemap +
geom_point(data = metadata, aes(x = `LonDD.v2`, y = `LatDD`, color = `repunit`))+
facet_wrap(~repunit)+
scale_color_manual(values = pal_species)
map_with_animation <- with_data +
transition_time(Year) +
ggtitle('Year: {frame_time}')
num_years <- max(metadata$Year) - min(metadata$Year) +1
animate(map_with_animation, nframes = num_years, fps = 1)
如果每年一帧适合,那么很容易将
Year
作为一个因素,并且会将级别2021
视为直接在级别2019
之后:
library(ggplot2)
library(gganimate)
df <- tibble::tibble(
year = rep(c(2015:2019, 2021, 2022), each = 12),
x = rep(1:12, times = 7),
y = runif(84, 1, 10)
)
df |>
ggplot(aes(x, y)) +
geom_line() +
transition_manual(factor(year)) +
ggtitle('Year: {current_frame}')
#> nframes and fps adjusted to match transition