3

I'm doing an animated graph with ggplot2 and gganimate where geom_line and geom_point represent the same numeric data. It is a scatterplot in which I want the point data appear with animated transition, as well as displaying an underlying line connecting the data points which is also animated.

Currently, 'geom_line' is correctly animated by transition_reveal(Año), but from there, I don't know how to animate geom_line.

I've tried adding transition_states (which I've used previously to successfully animate a geom_point element before), but I don't understand how to add two different types of transition to two different geoms on the same graph. Every time I try, I get errors. Elements such as enter_growdon't seem to do anything.

Here is the data:

Año = c(1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008)
Nacimientos= c(4571,4782,4834,4701,4787,4467,4571,4583,4776,4761,5017,5287)
Defunciones= c(889,946,946,916,991,1026,1040,1127,1092,1070,1156,1199)
TasaNatalidad= c(20.8, 21.0, 20.4, 19.1, 19.0, 17.2, 17.2, 16.8, 17.1, 16.6, 17.1, 17.6)
TasaMortalidad= c(4.0, 4.1, 4.0, 3.7, 3.9, 3.9, 3.9, 4.1, 3.9, 3.7, 3.9, 4.0)
TasaFecundidad= c(2.61, 2.63, 2.58, 2.43, 2.42, 2.21, 2.22, 2.18, 2.23, 2.17, 2.23, 2.29)

Tarapaca <- data.frame(Año,Nacimientos,Defunciones,TasaFecundidad,TasaMortalidad,TasaNatalidad)

This is the code that renders an animated graph with moving lines, but the circles just "appear" without animation:

library(ggplot2)
library(gganimate)

n0 <- ggplot(Tarapaca, aes(x = Año, y = Nacimientos)) +
            geom_line(color ="#BA3C11", alpha=0.6) +
                  transition_reveal(Año) +
            geom_point(aes(group = seq_along(Año), 
                           size = TasaNatalidad, 
                           alpha=TasaFecundidad), 
                           color ="#DD4814") +
                  enter_grow() + 
                  enter_fade() +
                  scale_size(range = c(4, 10)) +
                  scale_alpha(range = c(0.2, 1)) + 
            ease_aes('cubic-in-out') +
            labs(x = "Años", y = "Nacimientos") +
            theme(axis.text.x = element_text(angle = 45, vjust = 0.5)) +
            scale_x_continuous("Año", labels = as.character(Año), breaks = Año) + 
            labs(size="Tasa de natalidad", alpha ="Tasa de fecundidad") + 
            theme(panel.grid.major = element_line(Año, color = "white"),
                panel.grid.minor = element_blank(), # borrar líneas menores
                panel.background = element_rect(fill = "#F2E9E6"),
                legend.key = element_rect(fill = "#F2E9E6"))

n0  

[Resulting animation of first code chunk]

Running the previous code gives an graph that only animated lines, but I want to achieve both animated lines and points, and everything I've tried gives errors or doesn't work.

On the other hand, the following code renders an animated graph with animated circles or points, but I can't add lines to it, because as soon as I add a second geom it just returns errors:

NF <- ggplot(Tarapaca, aes(x = Año, y = Nacimientos)) + 
      geom_point(aes(size = TasaNatalidad, alpha=TasaFecundidad), color ="#DD4814") + 
      transition_states(Año, transition_length = 2, state_length = 1, wrap=FALSE) +
      shadow_mark() + 
      ease_aes('cubic-in-out') + 
        ggtitle('Año {closest_state}') + 
        scale_size(range = c(4, 10)) + 
        scale_alpha(range = c(0.2, 1)) +
        theme(axis.text.x = element_text(angle = 45, vjust = 0.5)) + 
        scale_x_continuous("Año", labels = as.character(Año), breaks = Año) + 
        labs(size="Tasa de natalidad", alpha ="Tasa de fecundidad") + 
        theme(panel.grid.major = element_line(Año, color = "white"), 
              panel.grid.minor = element_blank(), 
              panel.background = element_rect(fill = "#F2E9E6"), 
              legend.key = element_rect(fill = "#F2E9E6"))

NF

Resulting animation of second code chunk

It seems that geom_point requires transition_states and geom_line requires transition_reveal, but transition_states and transition_reveal are not compatible with each other.

How can I get both animations together (i.e., moving lines and animated circles) in a single graph?

  • have a look at https://gganimate.com/reference/transition_layers.html - I am working on a similar problem and i think this may be the route to go – Canovice Jul 26 '19 at 19:34

0 Answers0