By Nicola Pasquino

2016-05-23 17:42:32 8 Comments

Skimming through the options of gganimate that can be set when gg_animate() is called to render the animated sequence, it seems that there is no option to change the frame title so to make it clearer to the observer of what is the parameter that the frame is based on.

In other words, suppose that frame = year in a layer: how do I make the frame's title be year: #### where #### is the year of the current frame? Am I missing something or is it a limitation of the gganimate library?

How would you achieve the same result by a workaround? Thanks for your advice.


@eipi10 2016-05-23 17:50:09

Update for new gganimate API

gganimate has been redesigned with a new API. The frame title can now be animated with the code below. state_length and transition_length set the relative amount of time spent in a given "state" (meaning a given value of cyl here) and transitioning between states:

p = ggplot(mtcars, aes(wt, mpg)) + 
  geom_point() + 
  transition_states(cyl, transition_length=1, state_length=30) +
  labs(title = 'Cylinders: {closest_state}')

animate(p, nframes=40)

gganimate can be installed from github by running devtools::install_github('thomasp85/gganimate')

Original Answer

The frame's subset value is appended to any pre-existing title. You can therefore add a title with explanatory text. For example:


p = ggplot(mtcars, aes(wt, mpg, frame=cyl)) + geom_point() + 
    ggtitle("Cylinders: ")


As you can see in the GIF below, the prefix "Cylinders: " is now added to the title before the value of cyl:

enter image description here

@Nicola Pasquino 2016-05-24 20:38:47

This is exactly what I needed. Thanks! Can you point me to some other resources about gganimate?

@eipi10 2016-05-24 20:47:47

This was actually the first time I've used it so I haven't really looked. Here and here and here are a few brief tutorials.

@RDRR 2019-01-21 09:37:37

If using transition_time instead of transition_states you can swap '{closest_state}' for '{frame_time}'

@Brian Dolan 2020-05-31 15:36:38

I had to downvote this, there is no explanation as to what closest_state is or how to find the documentation on it, so the answer is not useful.

Related Questions

Sponsored Content

12 Answered Questions

[SOLVED] How to change legend title in ggplot

  • 2013-01-31 09:31:10
  • neversaint
  • 523295 View
  • 302 Score
  • 12 Answer
  • Tags:   r plot ggplot2

8 Answered Questions

[SOLVED] Rotating and spacing axis labels in ggplot2

  • 2009-08-25 21:05:24
  • Christopher DuBois
  • 772515 View
  • 684 Score
  • 8 Answer
  • Tags:   r ggplot2 labels

1 Answered Questions

12 Answered Questions

[SOLVED] How do I animate constraint changes?

13 Answered Questions

[SOLVED] How to join (merge) data frames (inner, outer, left, right)

20 Answered Questions

[SOLVED] Drop data frame columns by name

  • 2011-01-05 14:34:29
  • Btibert3
  • 1499808 View
  • 881 Score
  • 20 Answer
  • Tags:   r dataframe r-faq

1 Answered Questions

[SOLVED] Cumulative plots in gganimate

1 Answered Questions

[SOLVED] Sequentially Animate Changing Density Plots in R with gganimate

  • 2020-01-10 19:51:17
  • mechengjoe
  • 313 View
  • 0 Score
  • 1 Answer
  • Tags:   r ggplot2 gganimate

2 Answered Questions

[SOLVED] How to add specific text for each frame of an animation in R

2 Answered Questions

[SOLVED] How does gganimate order an ordered bar time-series?

Sponsored Content