2016-03-01 53 views
7

自从David Robinson发布了他的gganimate软件包以来,我一直在嫉妒和钦佩各种ggplot动画,并认为我会自己玩一玩。使用geom_bar时,我遇到gganimate问题。希望下面的例子演示这个问题。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) 

我试图张贴一些图片,但显然我没有足够的声誉,所以我希望这是有道理的,没有他们。这是一个错误,还是我错过了什么?

由于提前,

托马斯

回答

10

的原因是,如果没有小面,酒吧堆叠。使用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

为了避免这样的情况混乱,这是更为有用的fill更换frame(或colour,取决于你using`的GEOM):

p <- ggplot(df, aes(x = x, y = y, fill = z)) + 
    geom_bar(stat = "Identity") 
p 

enter image description here

两个地块即当您替换fillframe完全对应于专用一次绘制其中一种颜色。

+0

谢谢,这很有道理! – tecb1234