定义由gganimate创建的.gif的大小

Gau*_*tam 11 r ggplot2 gganimate

我正在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")
Run Code Online (Sandbox Code Playgroud)

以下是此代码的输出.

test.gif

小智 17

使用该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")
Run Code Online (Sandbox Code Playgroud)

  • 使用这种方法,有什么方法可以控制分辨率/dpi吗? (3认同)

Tje*_*ebo 14

尽管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")
Run Code Online (Sandbox Code Playgroud)

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

  • 更新,更新文档的拉取请求已被接受,因此现在至少在 gganimate 的开发版本上应该可用 (2认同)

spr*_*9er 9

使用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"
)
Run Code Online (Sandbox Code Playgroud)

  • 当我运行此代码时,我收到此错误错误:'image'参数不是magick图像对象. (3认同)
  • 现在,您可以像anim_save(“ test.gif”,gplot,width = 1000,height = 1000)一样使用`anim_save`` (2认同)