gpt4 book ai didi

r - geom_bar 的 gganimate 问题?

转载 作者:行者123 更新时间:2023-12-03 16:53:58 25 4
gpt4 key购买 nike

自从 David Robinson 发布他的 gganimate 软件包以来,我一直在羡慕和钦佩地看着 twitter 上出现的各种 ggplot 动画,并认为我自己会玩。我在使用 geom_bar 时遇到了 gganimate 问题。希望下面的例子演示了这个问题。

首先为可重现的示例生成一些数据:

df <- data.frame(x = c(1, 2, 1, 2),
y = c(1, 2, 3, 4),
z = c("A", "A", "B", "B"))

为了演示我正在尝试做的事情,我认为绘制一个普通的 ggplot 会很有用,由 z 分面。 .我试图让 gganimate 生成一个在这两个图之间循环的 gif。
ggplot(df, aes(x = x, y = y)) +
geom_bar(stat = "Identity") +
facet_grid(~z)

facetted_barchart

但是当我使用 gganimate 时,B 的情节表现得很奇怪。在第二帧中,条形从第一帧的条形结束时的值开始,而不是从原点开始。好像它是一个堆积条形图。
p <- ggplot(df, aes(x = x, y = y, frame = z)) +
geom_bar(stat = "Identity")
gg_animate(p)

bars_animation

顺便说一句,当用 geom_point 尝试相同的情节时一切都按预期工作。
q <- ggplot(df, aes(x = x, y = y, frame = z)) +
geom_point()
gg_animate(q)

我试图发布一些图片,但显然我没有足够的声誉,所以我希望没有它们也有意义。这是一个错误,还是我错过了什么?

提前致谢,

托马斯

最佳答案

原因是没有刻面,条形是堆叠的。使用 position = "identity" :

p <- ggplot(df, aes(x = x, y = y, frame = z)) +
geom_bar(stat = "Identity", position = "identity")
gg_animate(p)

enter image description here

为了避免在这种情况下混淆,替换 frame 会更有用。来自 fill (或 colour ,取决于您使用的几何体`):
p <- ggplot(df, aes(x = x, y = y, fill = z)) +
geom_bar(stat = "Identity")
p

enter image description here

当您替换 fill 时绘制的两个图来自 frame完全对应于一次只绘制一种颜色。

关于r - geom_bar 的 gganimate 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35731811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com