ggsave和gganimate动画中元件的大小一致 [英] Consistent size for symbols in ggsave and gganimate's 'animate'
本文介绍了ggsave和gganimate动画中元件的大小一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的最终目标是创建两个输出:
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
获得一致的符号大小,您必须将解析作为animate
的可选参数传递给png
,如下所示:
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)
添加:不确定您是否对此感兴趣,但是,我喜欢使用库(plotly)制作动画,因为默认情况下它会添加动画滑块。
以下是您的示例的ggplotly
方法:
library(plotly)
library(htmlwidgets)
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)) + theme_void() +
theme(panel.background = element_rect(fill = "pink")) +
geom_point(aes(frame = LDT))
p <- ggplotly(g) %>%
animation_opts(500, easing = "linear", redraw = FALSE)
saveWidget(p, file = "myAnimation.html", selfcontained = TRUE)
browseURL("myAnimation.html")
Here可以找到相关帖子。
这篇关于ggsave和gganimate动画中元件的大小一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文