1

Code below produces a waffle chart of a packet of skittles. I want to simulate a packet of skittles being eaten, with each skittle being chosen at random. Therefore I want to create an animated plot using gganimate of the number of circles reducing by 1 with each iteration of code, until there are no skittles left. How can I do this with gganimate?

I have tried creating a function, using slice_sample and various other ways, but in each case I struggled to set up the animation.

library(tibble); library(dplyr); library(ggplot2); library(waffle)

rep(c("purple", "pink", "red", "blue", "green"), 11) -> colour

tibble(colour) %>% 
  dplyr::count(colour, sort = TRUE, name = "n") ->
  skittles

ggplot(skittles, aes(label = colour, values = n, color = colour)) +
  geom_pictogram() +
  scale_label_pictogram(name = NULL, values = c(purple = "circle", pink = "circle", red = "circle", blue = "circle", green = "circle")) +
  scale_color_manual(name = NULL, values = c(purple = "purple", pink = "pink", red = "red", blue = "blue", green = "green")) +
  coord_equal() +
  theme_nothing() +
  guides(label = guide_legend(byrow = TRUE)) +
  theme(legend.spacing.y = unit(0.5, "cm"))

enter image description here

luciano
  • 13,158
  • 36
  • 90
  • 130

0 Answers0