Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change label of gganimate frame title

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.

like image 441
Nicola Pasquino Avatar asked May 23 '16 17:05

Nicola Pasquino


People also ask

How to change the frame title in GG_animate ()?

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.

How do I install gganimate?

gganimate can be installed from github by running devtools::install_github ('thomasp85/gganimate') The vignette has more details on how to use the new API. The frame's subset value is appended to any pre-existing title.

What is gganimate in R?

gganimate is an extension of the ggplot2 package for creating animated ggplots. It provides a range of new functionality that can be added to the plot object in order to customize how it should change with time. Key features of gganimate: transitions: you want your data to change.

What is gganimate stable version?

It provides a range of new functionality that can be added to the plot object in order to customize how it should change with time. gganimate stable version is available on CRAN and can be installed with install.packages ('gganimate').


1 Answers

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. closest_state means that while transitioning between states (which are the integer values of cyl in this case) the cyl value closest to the current transition value should be shown (e.g., since cyl can be 4, 6, or 8, values between, say, 4 and 5 are displayed as 4 and values between 5 and 6 are displayed as 6):

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')

The vignette has more details on how to use the new API.

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:

library(gganimate)

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

gg_animate(p)

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

like image 73
eipi10 Avatar answered Sep 17 '22 18:09

eipi10