【问题标题】:Define size for .gif created by gganimate - change dimension / resolution为 gganimate 创建的 .gif 定义大小 - 更改尺寸/分辨率
【发布时间】:2018-08-10 01:05:00
【问题描述】:

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

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

下面是一些示例代码:

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

下面是这段代码的输出。

【问题讨论】:

  • 我认为你想要 ani.options(ani.width=800, ani.height=400) 或将它们设置为任何你想要的。
  • @Tjebo 感谢您提出这个问题,如果其他选项效果更好,我会看看并更改已接受的答案。

标签: r ggplot2 gganimate


【解决方案1】:

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

我认为更好的解决方案是使用gganimate 中的animate() 函数创建一个对象,然后将其传递给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")

【讨论】:

  • 使用这种方法,有没有什么方法可以控制分辨率/dpi?
  • @radek 是的。看我的回答
【解决方案2】:

虽然Thomas suggests 看看animate,但遗憾的是文档在这方面不是很清楚。

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

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

基于示例provided by user 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")

尺寸为 450x300px,符合预期。

【讨论】:

  • 更新,更新文档的拉取请求已被接受,因此现在至少在 gganimate 的开发版本上应该可用
  • 这应该是最佳答案。
  • @dfrankow 不确定我理解你的问题吗?
  • 您在animate 中的回答是“res=150”。 animate 的文档没有关于“res”的描述,请参阅 gganimate.com/reference/animate.html。您可以链接描述 res 的参考吗?
  • 我明白了,谢谢!我不明白res 是设备的参数(即链接到上一段)。事实上,当我查看?grDevices::png 时,它准确地描述了res
【解决方案3】:

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

【讨论】:

  • 当我运行此代码时,我收到此错误错误:'image' 参数不是魔术图像对象。
  • 现在,你可以像anim_save("test.gif", gplot, width = 1000, height = 1000)一样使用anim_save
【解决方案4】:

您可以调整常规设置:

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

或更改每个命令的设置:

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

【讨论】:

  • ani.width & ani.height 现已贬值,请参阅下面的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
相关资源
最近更新 更多