Search code examples
rggplot2gganimate

How to animate this line graph


I have this line graph I created this plot p using ggplotStatic Plot

The code used to create the plot is below

p <- ggplot(teleco,aes(x = tenure, color = Churn))+ geom_freqpoly(size=2)+theme_minimal()

However I am wondering how best to animate it. I would like for the lines to fill in gradually. However I have tried this and it doesnt work as I believe I am not working with time.

p + transition_reveal(tenure)

When I run this code it comes through like so. What would be the best method to get it to animate to just have the lines fill in as they are in the static plot?

Animated Plot


Solution

  • My guess (it's hard to say for sure with a MRE) is that the unwanted behaviour comes from using geom_polygon.

    You could try instead manually grouping your data then using geom_line. Something like

    teleco %>%
      group_by(tenure, Churn) %>%
      summarise(count = n(), .groups = 'drop') %>%
      ggplot(aes(tenure, count, col = Churn)) +
        geom_line(size = 2)