ggsave和gganimate的“动画”中符号的一致大小

问题描述 投票:1回答:1

我的最终目标是创建两个输出:

1]显示我所有数据的静态图像,另存为png2)我的数据的动画,另存为gif

我正在使用ggplot2gganimate,但我对为什么两个保存方法之间的符号大小不一致感到困惑。

我尝试调整dpi并另存为jpg而不是png,但没有运气。 有人可以帮助我弄清楚如何使两个输出对象的宽度,高度和符号大小保持一致吗?

这是显示两个输出的可复制示例。您可以在gif中看到黑点较小。

制作png

library(gganimate)
library(ggplot2)

locs <- data.frame(x = c(1, 2, 3, 4, 5, 6),
                   y = c(1, 2, 3, 3.1, 3.2, 6),
                   LDT = c(1, 2, 3, 4, 5, 6))

g <- ggplot(locs, aes(x, y)) +
  geom_point() +
  theme_void() +
  theme(plot.background = element_rect(fill = "pink"))
g
ggsave("test.png", g, width = 2, height = 2, dpi = 100)

enter image description here

制作gif

anim <- g + transition_time(LDT)
animate(anim, duration = 1, fps = 20, width = 200, height = 200)
anim_save("test.gif")

enter image description here

r png gif resolution gganimate
1个回答
0
投票

[animate()默认情况下使用png()生成帧。

在您的ggsave呼叫中,您指定了100 dpi的打印分辨率。

要使用png获得相同的结果,您必须设置res = 100(请参阅test_png_device.png)。

因此,使用animate具有一致的符号大小,您必须将分辨率作为png的可选参数传递给animate,如下所示:

library(gganimate)
library(ggplot2)
library(gifski)

locs <- data.frame(x = c(1, 2, 3, 4, 5, 6),
                   y = c(1, 2, 3, 3.1, 3.2, 6),
                   LDT = c(1, 2, 3, 4, 5, 6))

g <- ggplot(locs, aes(x, y)) +
  geom_point() +
  theme_void() +
  theme(plot.background = element_rect(fill = "pink"))

ggsave("test.png", g, width = 2, height = 2, dpi = 100)

png(filename = "test_png_device.png", width = 200, height = 200, units = "px", res = 100)
g
dev.off()

anim <- g + transition_time(LDT)
myAnimation <- animate(anim, duration = 1, fps = 20, width = 200, height = 200, renderer = gifski_renderer(), res = 100)
anim_save("test.gif", animation = myAnimation)

ggsave Result

© www.soinside.com 2019 - 2024. All rights reserved.