2016-03-01 49 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