如何固定 gganimate 中绘图的宽度?

How to fix the width of the plot in gganimate?

我在设置动画时无法设置绘图的宽度。

因此,如果我使用库 gapminder 制作静态图,代码如下:

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

它使情节成为 window 的全宽,就像我期望的那样。但是如果我添加转换:

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

它使绘图宽度大约减少了一半。

有没有办法让它成为动画的全宽?

您只需调整绘图的 heightwidth,您可以通过稍微调整代码的最后一部分来使用 animate 来完成此操作:

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

heightwidth 选择的数字在我的 RStudio 中显示为默认值,因此您可能需要将它们调整为您想要的任何值。