我的最终目标是创建两个输出:
1]显示我所有数据的静态图像,另存为png
2)我的数据的动画,另存为gif
。
我正在使用ggplot2
和gganimate
,但我对为什么两个保存方法之间的符号大小不一致感到困惑。
我尝试调整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)
gif
anim <- g + transition_time(LDT)
animate(anim, duration = 1, fps = 20, width = 200, height = 200)
anim_save("test.gif")
[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)