3

I have trouble setting the width of the plot when I animate it.

So if I make a static plot for example using the library gapminder with a code like this:

library(ggplot2)
library(gganimate)
theme_set(theme_bw())
library(gapminder)

p <- ggplot(
  gapminder, 
  aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)
) +
  geom_point(show.legend = FALSE, alpha = 0.7) +
  scale_color_viridis_d() +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  labs(x = "GDP per capita", y = "Life expectancy")
p

it makes the plot full width of the window, just like I expect it to do. But If I add transition:

p + transition_time(year) +
  labs(title = "Year: {frame_time}")

it makes the plot width about half the size.

Is there a way to make it full width for the animation?

Arienrhod
  • 2,451
  • 1
  • 11
  • 19
tumavee
  • 65
  • 1
  • 5

1 Answers1

2

You just have to adjust the height and width of the plot and you can do so with animate by slightly adjusting the last part of your code:

p <- p + transition_time(year) +
  labs(title = "Year: {frame_time}")
animate(p, height = 461, width = 644)

The chosen numbers for height and width appear as defaults in my RStudio, so you'll probably have to adjust them to whichever values you had in mind.

Arienrhod
  • 2,451
  • 1
  • 11
  • 19
  • Man, I must have done this a thousand times (before I even asked this question here) and it had stated something like "animation of gg objects not supported". Then I noticed the slight change in your example. It works now! Thank you! – tumavee Aug 07 '19 at 08:51