为gganimate创建的.gif定义尺寸-更改尺寸/分辨率

问题描述 投票:15回答:5

我正在使用gganimate创建一些要插入到报告中的.gif文件。我可以保存文件并可以正常查看,但是我发现显示的大小很小:480x480。有没有办法调整它-可能沿着height中的widthggsave()自变量行?

我可以放大,但对质量的影响不佳,在我的用例中很难理解。

这里有一些示例代码:

gplot <- 
  ggplot(gapminder, 
         aes(x = gdpPercap, y = lifeExp, colour = continent, 
             size = pop, 
             frame = year)) +
    geom_point(alpha = 0.6) + 
    scale_x_log10()

gganimate(gplot, "test.gif")

下面是此代码的输出。

test.gif

r ggplot2 gganimate
5个回答
1
投票

您可以调整常规设置:

animation::ani.options(ani.width= 1000, ani.height=1000, ani.res = 1000)

或更改每个命令的设置:

gganimate(gplot, ani.width= 1000, ani.height=1000, "test.gif")

25
投票

使用magick包可能会出现问题。

[我认为更好的解决方案是使用animate()中的gganimate函数创建一个对象,然后将该对象传递给anim_save()函数。无需使用其他软件包。

library(gganimate)
library(gapminder)

my.animation <- 
  ggplot(
  gapminder,
  aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop)
 ) +
geom_point(alpha = 0.6) +
scale_x_log10() +
transition_time(year)

# animate in a two step process:
animate(my.animation, height = 800, width =800)
anim_save("Gapminder_example.gif")

10
投票

使用gganimate包的新API,是

gganimate

0
投票

确认@ spren9er的答案有效:

library(gganimate) library(gapminder) gplot <- ggplot( gapminder, aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop) ) + geom_point(alpha = 0.6) + scale_x_log10() + transition_time(year) magick::image_write( animate(gplot, width = 1000, height = 1000), "test.gif" )


0
投票

尽管anim_save("test.gif", gplot, width = 1000, height = 1000)可以查看Thomas suggests,但是很遗憾,文档在这方面不是很清楚。

一个可以通过指定animate参数直接控制分辨率。并且也可以使用不同的res。我个人喜欢按英寸控制我的数字,并以此为基础控制分辨率。对我来说,这样做的好处是,字体大小的惊喜将大大减少,并且图形的质量当然会更好。

基于units提供的示例。

user Nathan

尺寸为450x300px,按预期。library(gganimate) library(gapminder) my.animation <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop)) + geom_point(alpha = 0.6) + scale_x_log10() + transition_time(year) + theme_bw(base_size = 8) animate(my.animation, height = 2, width = 3, units = "in", res = 150) anim_save("gapminder_example.gif")

© www.soinside.com 2019 - 2024. All rights reserved.