I'm trying to build some teaching illustrations that illustrate how we sort points to build the emperical cumulative distribution function (ECDF). I thought it would be nifty to use gganimate
to show the points sliding from an unsorted state into a sorted state. I'm new to gganimate
so I pulled up some examples and gave it a go.
Below is my first attempt. I created a data frame that contains an unsorted set of points. I tagged these sort_group=1
then I sort the points and assign new sort_order
and set sort_group=2
in the data. I thought this would allow me to transition on sort_group
to go from the unsorted to the sorted.
library(gganimate)
library(tidyverse)
df <- tibble(vals = rnorm(100), key = 1:100) %>%
mutate(sort_order = row_number(), sort_group=1)
df %>%
bind_rows(df %>% arrange(vals) %>% mutate(sort_order = row_number(), sort_group=2)) ->
combined_data
ggplot(combined_data) +
aes(x = vals, y = sort_order, color=key) +
geom_point() +
theme_bw() +
transition_time(sort_group) +
ease_aes('linear') ->
p
animate(p, nframes = 75, renderer = gifski_renderer("gganim.gif"))
There's a problem with the above though... the dots are sliding along the x axis as if their vals
are changing. But each point keeps the same key
and the same vals
but gets a new sort_order
. So I would expect the see the points sliding vertically, some slide up, others down, into their new sort orders.
I'm quite sure the problem is that I am missing some basic concept of ggamimate
but I can't figure out what I'm doing wrong. Any tips?