24

我正在使用gganimate创建一些要插入到报告中的 .gif 文件。我可以保存文件并很好地查看它们,但是,我发现显示的尺寸很小:480x480。有没有办法调整它 - 也许沿着heightwidth论点ggsave()

我可以放大,但这对质量的影响很差,并且对于我的用例来说相当难以阅读。

这是一些示例代码:

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")

以下是此代码的输出。

测试.gif

4

4 回答 4

35

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

我认为更好的解决方案是使用animate()函数 ingganimate创建一个对象,然后将其传递给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")
于 2018-09-10T00:48:30.737 回答
16

尽管Thomas 建议看一下animate,但遗憾的是文档在这方面不是很清楚。

?animate表明可以通过参数指定设备...参数。?grDevices::png您可以在或找到可用的参数?grDevices::svg

您可以通过指定res参数直接控制分辨率。并且一个也可以使用不同的单位。我个人喜欢以英寸为单位控制我的图形尺寸,并以此控制分辨率。对我来说好处是,字体大小的惊喜会少得多,当然图形的质量会更好。

基于用户 Nathan 提供的示例。

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")

正如预期的那样,尺寸为 450x300 像素。 在此处输入图像描述

于 2020-05-12T23:14:30.933 回答
10

使用gganimate包的新API,它是

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"
)
于 2018-07-21T22:57:26.000 回答
3

您可以调整常规设置:

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

或更改每个命令的设置:

gganimate(gplot, ani.width= 1000, ani.height=1000, "test.gif")
于 2018-03-31T15:05:14.690 回答