gganimate 注释不在 transition_states 中的日期变量

gganimate annotate a date variable that is not in the transition_states

尝试使用 ggplot 和 gganimate 创建动画图表,其中 x 轴(和 transition_states 值)是数字,但 运行 日期变量作为注释覆盖在图表上。动画有效,但似乎时间序列中的所有日期都是一次打印(全部在彼此之上)而不是与动画相对应。还试图在 geom_point 上方保持一致的 ggrepel 标签。这是一个代表:

library(dplyr)
library(lubridate)
library(ggrepel)
library(ggplot2)
library(gganimate)

a0 <- data.frame(dt = seq(ymd('2020-01-01'),ymd('2020-03-31'), by = '1 day'),
                 num = seq(1,91,by=1), val=seq(1,91,by=1), label = rep("x",91))

p <- ggplot(data=a0, aes(x=num, y=val, color=label)) +
  geom_point(size=10) +
  annotate("text",  x = 75, y = 75,
           label = as.Date(a0$dt), size = 10) +
  geom_text_repel(label=a0$label, size=10) +
  scale_x_continuous() +
  theme_minimal() +
  theme(text = element_text(size=30),
        legend.position = "none")

anim <- p + 
  transition_states(num,
                    transition_length = 10,
                    state_length = 1000)

anim

有趣的问题。

检查下面的 geom_text 行。

p <- ggplot(data=a0, aes(x=num, y=val, color=label, group=num)) +
    geom_point(size=10) +
    geom_text(data=a0, aes(x=75, y=75, label=as.Date(a0$dt), size=10)) +
scale_x_continuous() +
theme_minimal() +
theme(text = element_text(size=30),
    legend.position = "none")

希望对您有所帮助。