在 R 中使用 gganimate 包中的 transition_levels 时使用 closest_level 更改标题

Using closest_level to change titles while using transition_levels from the package gganimate in R

我正在使用 gganimate 中的 transition_layers 创建动画情节,其标题在每一层都更新。这是一个简单的两层可重现示例。

library(gganimate)

## data
df = data.frame(x=c(0,1), 
                y=c(0,1))

## static plot
g = ggplot(data=df, aes(x=x, y=y))+
  geom_point()+
  geom_line()+
  labs(title = '{closest_layer}')
g

## animated plot
gg = g +
  transition_layers(layer_names=c('Layer 1: Points', 
                                  'Layer 2: Line')) + 
  enter_fade()
a = animate(gg, nframes = 50, end_pause = 20)
a

它按预期开始工作,但在动画结束时,标题过早变回 'Layer 1: Points'(同时仍显示第二层,并且在动画重新开始之前)。如何在显示第二层时让标题停留在“第二层”?

我想通了。使用 next_layer 而不是 closest_layer 就可以了。

library(gganimate)

## data
df = data.frame(x=c(0,1), 
                y=c(0,1))

## static plot
g = ggplot(data=df, aes(x=x, y=y))+
  geom_point()+
  geom_line()+
  labs(title = '{closest_layer}')
g

## animated plot
gg = g +
  transition_layers(layer_names=c('Layer 1: Points', 
                                  'Layer 2: Line')) + 
  enter_fade()
a = animate(gg, nframes = 50, end_pause = 20)
a