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

标签 r ggplot2 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

它使绘图占满整个窗口的宽度,就像我期望的那样。但是如果我添加过渡:

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

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

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

最佳答案

您只需调整绘图的高度宽度,您可以使用animate稍微调整最后一部分你的代码:

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

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

关于r - 如何固定 gganimate 中绘图的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57388625/

相关文章:

R ggplot2 : filling the space between 2 lines with varying x values (geom_ribbon) and formatting log axis labels

r - 如何在gganimate中显示y轴的当前值?

r - 除了动画直方图之外,如何向 gganimate 添加移动 vline?

r - 使用ggmap和gganimate创建 'flyover' map 动画

r - 让我的绘图显示更美观

r - stat_contour binwidth 奇数

R data.table 条件在组内,但在组中的第一个实例中记录

r - 如何转换条形图的 y 轴 (ggplot2)

r - 拆分列表每 n 个元素和 cbind,然后 rbind 切片

r - 将自定义函数与 tidyverse 结合使用