tumavee
tumavee

Reputation: 85

How to fix the width of the plot in gganimate?

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?

Upvotes: 3

Views: 1713

Answers (1)

Arienrhod
Arienrhod

Reputation: 2581

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.

Upvotes: 3

Related Questions