我正在使用gganimate
创建一些要插入到报告中的.gif文件。我可以保存文件并可以正常查看,但是我发现显示的大小很小:480x480。有没有办法调整它-可能沿着height
中的width
和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")
下面是此代码的输出。
您可以调整常规设置:
animation::ani.options(ani.width= 1000, ani.height=1000, ani.res = 1000)
或更改每个命令的设置:
gganimate(gplot, ani.width= 1000, ani.height=1000, "test.gif")
使用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")
使用gganimate
包的新API,是
gganimate
确认@ 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"
)
尽管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")