【发布时间】:2016-08-01 06:31:10
【问题描述】:
我正在制作一个应该保存为 gif 的人口金字塔。 Kind of like in this tutorial of Flowing Data,但使用 ggplot 而不是 plotrix。
我的工作流程:
1) 创建人口金字塔
2) 在for-loop 中创建多个金字塔图
for (i in unique(d$jahr)) {
d_jahr <- d %>%
filter(jahr == i)
p <- ggplot(data = d_jahr, aes(x = anzahl, y = value, fill = art)) +
geom_bar(data = filter(d_jahr, art == "w"), stat = "identity") +
geom_bar(data = filter(d_jahr, art == "m"), stat = "identity") +
coord_flip() +
labs(title = paste(i), x = NULL, y = NULL)
ggsave(p,filename=paste("img/",i,".png",sep=""))
}
3) 使用 animation 包将绘图保存为 gif
我的问题:
所有年份都有不同的值,因此 x 轴有不同的范围。这会导致 gif 中出现奇怪的外观,因为图的中心会向右、向左、向右跳跃……
是否可以在独立创建的多个绘图上固定 x 轴(在本例中为 y 轴,因为 coord-flip())?
【问题讨论】:
-
请阅读how do I ask a good question 和proding a minimal reproducible example 并相应地编辑您的帖子。即,提供输入数据(虚拟或真实)并将代码减少到属于问题一部分的行(例如,prob 是 ggplot,而不是保存 jpeg)。
-
@zx8754 gganimate 似乎是个好主意,谢谢