Gau*_*tam 11 r ggplot2 gganimate
我正在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")
Run Code Online (Sandbox Code Playgroud)
以下是此代码的输出.
小智 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)
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)
使用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)