Is there a way to use transition_reveal()
in gganimate to draw lines one by one - or a workaround using other transition functions..
library(tidyverse)
library(gganimate)
set.seed(1)
d <- tibble(x = 1:100, a = cumsum(rnorm(100)), b = cumsum(rnorm(100))) %>%
pivot_longer(cols = a:b, names_to = "grp", values_to = "y")
d
# # A tibble: 200 x 3
# x grp y
# <int> <chr> <dbl>
# 1 1 a -0.626
# 2 1 b -0.620
# 3 2 a -0.443
# 4 2 b -0.578
# 5 3 a -1.28
# 6 3 b -1.49
# 7 4 a 0.317
# 8 4 b -1.33
# 9 5 a 0.646
# 10 5 b -1.99
# # ... with 190 more rows
I would like for only line to be revealed at a time - so that the plotting for the second line commences after the first is complete (on frame 101) - rather than both lines being plotted at the same time...
ggplot(data = d, mapping = aes(x = x, y = y, colour = grp)) +
geom_line() +
transition_reveal(along = x)
We could make a helper column that puts the points for b
after all the points for a
:
d %>%
arrange(grp, x) %>%
mutate(x_reveal = row_number()) %>%
ggplot(aes(x = x, y = y, colour = grp)) +
geom_line() +
transition_reveal(along = x_reveal)