2

Ok, we all know this is not my real data or question, but, for the sake of a reproducible example...

Let's say that I'm trying to get enough flowers for my daughter's wedding.

My friend Anna has 25 plants of each species in the iris dataset at her house. I want to impress the maid of honor by showing her I found an EXTRA 25 plants of each species at my friend Jay's. In the graph, I'm also going to show the condition of the plants, because maybe we don't want to use crappy flower's at my daughter's wedding.

  • First, I want to show her the graph of JUST Anna's flowers.
  • Then, I'm going to wow her by smoothly "growing" the bar chart upwards to show her that we actually have 50 of each species.

I'm stumped on how to achieve this simple animation using gganimate!

Here's the code to create the "starting point" (left-most graph) and the "end point" (right hand graph). How can I animate between the two?

library(tidyverse)
library(gganimate)

House <- rep(c(rep(c("Anna's", "Jay's"), each = 25)))
Houses <- c(House, House, House)

Plant_Condition <- rnorm(150, mean = 20, sd = 10)
Plant_Condition <- ifelse(Plant_Condition > 25, "good", "poor")

iris_mod <- iris %>% 
  mutate(House = Houses,
         Condition = Plant_Condition)

p <- ggplot(iris_mod %>% filter(House == "Anna's"), aes(Species, fill = Condition)) +
  geom_bar() +
  coord_cartesian(ylim = c(0, 50))

p2 <- ggplot(iris_mod, aes(Species, fill = Condition)) +
  geom_bar()  +
  coord_cartesian(ylim = c(0, 50))

Iris plots

Nova
  • 5,423
  • 2
  • 42
  • 62
  • Have you read about transition_filter in the gganimate package? https://gganimate.com/reference/transition_filter.html – Susan Switzer Nov 13 '20 at 22:06
  • Thanks Susan! I have - it's why I used the word "filter" in my post. Couldn't get it to work though, especially since it requires more than one filtering condition. Good hint, maybe someone with more experience with it will help out here. – Nova Nov 13 '20 at 22:24

1 Answers1

1

I find the easiest way to do this kind of thing with gganimate is to imagine you are creating a plot with facets. If you can achieve the two frames you want as facets, you can instead transition between them. So here we can do:

iris_mod <- rbind(iris_mod %>% filter(House == "Anna's"), 
                  within(iris_mod, House <- "Everything"))

ggplot(iris_mod, aes(Species, fill = Condition)) +
  geom_bar() +
  coord_cartesian(ylim = c(0, 50)) +
  transition_states(House,
                    transition_length = 2,
                    state_length = 2)

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • That's stellar! I'll mark as accepted, unless I get an answer that shows how to do this without creating another data.frame in the next couple days... – Nova Nov 13 '20 at 22:26