Dan
Dan

Reputation: 372

Issue Creating a Flowing Sankey Diagram in R Using gganimate

I am using the following guide in an attempt to create a flowing sankey diagram: https://www.hvitfeldt.me/blog/recreate-sankey-flow-chart/#r-packages. My understanding is that this approach (i.e. gganimate(p)) utilizes an old and now unsupported version of gganimate. I am attempting to adapt the code to work with the new gganimate. I am using the code provided below.

I expected a result like the one on the the blog: enter image description here

i.e. slow moving and with individual points. What I get with my code are fast moving "points" with each point actually being represented by a series of five points:

enter image description here

Any help replicating the blog product using the new gganimate would be much appreciated.

CODE:

library(tidyverse)
library(gganimate)

sigmoid <- function(x_from, x_to, y_from, y_to, scale = 5, n = 100) {
  x <- seq(-scale, scale, length = n)
  y <- exp(x) / (exp(x) + 1)
  tibble(x = (x + scale) / (scale * 2) * (x_to - x_from) + x_from,
         y = y * (y_to - y_from) + y_from)
}

n_points <- 400
data <- tibble(from = rep(4, n_points),
               to = sample(1:4, n_points, TRUE),
               color = sample(c("A", "B"), n_points, TRUE)) 

p <- map_df(seq_len(nrow(data)), 
            ~ sigmoid(0, 1, as.numeric(data[.x, 1]), as.numeric(data[.x,                     
 2])) %>%
          mutate(time = row_number() + .x,
                 y = y + runif(1, -0.25, 0.25))) %>%
  ggplot(aes(x, y, frame = time)) +
    geom_point()+transition_time(time) 

p

Upvotes: 0

Views: 761

Answers (1)

Jon Spring
Jon Spring

Reputation: 66835

It looks like you have 499 values in time, more than the default number of frames that get rendered (100). When you use transition_time(time) it's including all the data, even for frames "in between" the ones that are rendered. That's why you're seeing groups of 5 dots.

You can either:

1) replace with transition_manual(time) so it just uses 100 frames of your data, discarding the ones in between, or

2) keep the transition_time(time) but render using animate(p, nframes = 499), so that you keep all the frames.

enter image description here

Upvotes: 3

Related Questions