1

I am trying to replicate the beta distribution animation of Wikipedia in R.

beta distribution animation of Wikipedia

Originally, I started with the following code

ggplot() +
  xlim(0, 1) +
  geom_function(fun = dbeta, args = list(shape1 = 5, shape2 = 5))

But I did not manage to use gganimate to animate the plot based on the shape parameters.

Therefore I tried a second approach where I first generate the data and then plot it using geom_line() (similar as in this post)

library(tidyverse)
library(gganimate)

# get beta PDF for specific parameters ------------------------------------
calc_beta <- function(alpha, beta){
  X <- seq(0.01, 0.99, 0.01)
  PDF <- dbeta(X, shape = alpha, shape2 = beta)
  tibble(X, PDF)
}

# alpha and beta values ---------------------------------------------------
n <- 50
r <- exp(seq(log(0.1), log(5), length.out = n))
alpha <- c(rev(r), r, rep(5, 2*n), rev(r), r)
beta <- c(rep(5, 2*n), rev(r), r, rev(r), r)

# tibble with alpha, beta and PDF -----------------------------------------
dat <- tibble(alpha = alpha, beta = beta) %>% 
  mutate(time = 1:n()) %>% 
  group_by(time) %>%
  mutate(plotdata = map2(alpha, beta, calc_beta)) %>% 
  unnest(plotdata)

# plot --------------------------------------------------------------------
p <- dat %>% 
  ggplot(aes(x = X, y = PDF)) +
  geom_line() +
  transition_time(time) +
  coord_cartesian(ylim = c(0, 4)) +
  annotate('text', x = 0.5, y = 4,
           label = 'alpha = {round(alpha[as.integer(frame_time)], 2)}
          beta = {round(beta[as.integer(frame_time)], 2)}') +
  labs(title = 'alpha = {round(alpha[as.integer(frame_time)], 2)}
          beta = {round(beta[as.integer(frame_time)], 2)}')
animate(p, nframes = 400, fps = 40)

enter image description here

Now I have 3 Qustions

  1. Is there a way to use gganimate to animate the args of geom_function (first approach)?
  2. Is there an easier solution then the second approach?
  3. Why does the animate labeling not work within the annotate() function but only within the labs() function?
retodomax
  • 434
  • 3
  • 14
  • 1
    1) I don't think so. To my knowledge gganimate animates aesthetics and not parameters. 2) your approach looks fairly straight forward to me. I don't think there will be a better option 3) annotate is a "fixed" layer and will not change with your animation (again, no aesthetics!). You could use geom_text instead, but you will need to create a data frame with one label for each state. – tjebo Jun 29 '22 at 10:29
  • 1
    or you can use a stat where fun is an aesthetic... https://stackoverflow.com/a/70636329/7941188 – tjebo Jun 29 '22 at 10:35

0 Answers0