2

I'm trying with ggplot2 to create an animated pie plot.

My data is a bit complex but here is a simplified example :

ex = data.frame(cat=c("cat1","cat2","cat1","cat2","cat1","cat2"), f = c(70,30,60,40,50,50), t=c(1,1,2,2,3,3))
ex$t = factor(ex$t)

p = ggplot(ex, aes(x="", y=f, fill=cat, frame=t))+
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start=0) 

gganimate(p, "ex.gif", interval=1)

If I display p, it seems ok : simple png

But the gif is not ok : gif

Any idea how to solve this bug ?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Pad
  • 33
  • 3
  • Have a look at [this](https://stackoverflow.com/questions/41453746/how-to-get-complete-rather-than-partial-pie-charts-using-gganimate) post. Hope it helps. – markus Dec 18 '17 at 21:50

1 Answers1

1

The generated .gif looks correct to me. Each frame is generated with separate data. Perhaps you are looking for gradually generating pie chart. You should use cumulative = TRUE to build frames cumulative.

The code should be changed to:

p = ggplot(ex, aes(x="", y=f, fill=cat, frame=t, cumulative = TRUE))+
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start=0) 
MKR
  • 19,739
  • 4
  • 23
  • 33
  • the png image should be one of the gif frames. The pie should be full because numbers all add up to 100. Isnt it ? – Pad Dec 18 '17 at 02:06
  • The full pie shows 300. Each observation will add up to 300. If cumulative is TRUE then pie will be full at 3rd frame. – MKR Dec 18 '17 at 05:19