Search code examples
rnumbersjquery-animatecounter

Is there an R function to animate a number counter?


I was wondering if anyone knows how to animate a number counter in R. Or somewhere else if that is possible? I want a number to go from one number to the next whilst increasing in size.

For example, to go from 1 to 456 whilst counting but also increasing in size whilst doing this.

Thanks a lot for your help, Best wishes.


Solution

  • Not entirely sure on how you plan on using the number counter. If this just about creating an animation of a number counter, you could use gganimate.

    Here is an example of a counter increasing up to 20.

    library(ggplot)
    library(gganimate)
    
    n_max <- 20L
    p <- ggplot(data.frame(n = 1L:n_max)) +
        geom_text(aes(x = 0, y = 0, label = as.integer(n), size = n)) +
        theme_void() +
        guides(size = "none") +
        scale_size_area(max_size = 50) +
        transition_time(as.integer(n))
    
    an <- animate(p, nframes = n_max, duration = 3)
    anim_save("animation.gif", an)
    

    enter image description here

    If you increase n_max you'll need to adjust the number of frames (and probably the FPS) to meet your needs regarding smoothness of the animation.