Skip to content

Instantly share code, notes, and snippets.

@erichare
Created February 26, 2019 19:40
Show Gist options
  • Save erichare/31e23e7c664b5a04cb783b08377681a0 to your computer and use it in GitHub Desktop.
Save erichare/31e23e7c664b5a04cb783b08377681a0 to your computer and use it in GitHub Desktop.
An iris tour using gganimate
library(tidyverse)
library(gganimate)
library(cowplot)
iris_tall <- iris %>%
cbind(iris %>%
rename_all(function(.) paste0(., "_2")) %>%
select(-Species_2)) %>%
gather(key = Variable, value = Value, c(1:(ncol(iris) - 1))) %>%
gather(key = Variable2, value = Value2, c(2:ncol(iris))) %>%
mutate(Variable2 = gsub("_2", "", Variable2)) %>%
filter(Variable != Variable2) %>%
mutate(Index = droplevels(interaction(Variable, Variable2, sep = " vs. ")))
p1 <- ggplot(data = iris_tall, aes(x = Value, y = Value2, colour = Species)) +
geom_point() +
transition_states(
Index,
transition_length = 1,
state_length = 1
) +
labs(title="{closest_state}") +
ease_aes('linear') +
theme_minimal(16) +
enter_fade() +
exit_fade()
p1_animate <- animate(p1, height = 600, width = 800, fps = 30, duration = 30)
anim_save("iris_tour.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment