Search code examples
rgganimate

Displaying data value in subtitle of gganimate plot [R]


Since I couldn't find a answer here a simplified version of my code and used data.

My data contains a "days" variable, which represents the number of originally datapoints in this week. Using a gganimate plot, I can display the current week in the animation, but not the associating aggregated data points ("days").

Sample data:

df <- structure(list(GKZ = c(101L, 101L, 101L, 101L, 101L, 101L, 101L, 
                   101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 
                   101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 
                   101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 
                   101L, 101L, 101L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 
                   102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 
                   102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 
                   102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 
                   102L, 102L), week = c(8, 9, 10, 11, 12, 13, 14, 15, 
                                                  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 
                                                  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 
                                                  48, 49, 50, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
                                                  21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 
                                                  37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50), district = c("Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", "Eisenstadt(Stadt)", 
                                                                                                                      "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", 
                                                                                                                      "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", 
                                                                                                                      "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", 
                                                                                                                      "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", 
                                                                                                                      "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", 
                                                                                                                      "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", 
                                                                                                                      "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", 
                                                                                                                      "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)", 
                                                                                                                      "Rust(Stadt)", "Rust(Stadt)", "Rust(Stadt)"), deaths = c(0L, 0L, 
                                                                                                                                                                             0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                                             0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                                             0L, 0L, 0L, 0L, 0L, 0L, 3L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                                             0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                                             0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                                                                                             0L, 0L, 0L, 0L), pop = c(14816L, 14816L, 14816L, 14816L, 
                                                                                                                                                                                                               14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 
                                                                                                                                                                                                               14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 
                                                                                                                                                                                                               14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 
                                                                                                                                                                                                               14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 
                                                                                                                                                                                                               14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 14816L, 1980L, 
                                                                                                                                                                                                               1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 
                                                                                                                                                                                                               1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 
                                                                                                                                                                                                               1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 
                                                                                                                                                                                                               1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 
                                                                                                                                                                                                               1980L, 1980L, 1980L, 1980L, 1980L, 1980L), days = c(5L, 7L, 7L, 
                                                                                                                                                                                                                                                                   7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                                                                                                                                                                                                                                                   7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                                                                                                                                                                                                                                                   7L, 7L, 7L, 7L, 7L, 7L, 7L, 4L, 5L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                                                                                                                                                                                                                                                   7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                                                                                                                                                                                                                                                   7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                                                                                                                                                                                                                                                   7L, 7L, 4L), deathrate = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                                                                                                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                                                                                                               0, 0, 0, 0, 0, 0, 0, 0, 20.2483801295896, 20.2483801295896, 0, 
                                                                                                                                                                                                                                                                                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                                                                                                               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                                                                                                               0)), row.names = c(NA, -86L), groups = structure(list(GKZ = 101:102, 
                                                                                                                                                                                                                                                                                                                                                     .rows = structure(list(1:43, 44:86), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                                                                                                                                                                                                                        "vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "tbl_df", "tbl", "data.frame"))

My actual plot uses a join of the full dataset on a shapefile. I hope this sample plot is enough to explain my question. How can I properly refer to each row of the data in the subtitle argument? Using the following code, only prints the first "days" value.

library(tidyverse)
library(gganimate)    
df %>%
      ggplot(aes(district, deathrate))+geom_col()+
      labs(title = "Week: {current_frame}",
           subtitle = paste0("Number of days: ", {df$days}))+
      transition_manual(week) + ease_aes("linear")

Since gganimate uses glue I also tried the glue notation, but this returns in other errors...

subtitle = "Number of days: {days}"

subtitle = "Number of days: {df$days}"

I would also be happy, if it can be done outside of the subtitle argument :)


Solution

  • This could be achieved by filtering the dataset and pulling out the desired variable. When doing so I received an error as your dataset contains multiple rows (districts) per frame. Therefore I used .[[1]] to pull out the first value.

    library(tidyverse)
    library(gganimate)    
    df %>%
      ggplot(aes(district, deathrate))+geom_col()+
      labs(title = "Week: {current_frame}",
           subtitle = "Number of days: {filter(df, week == current_frame) %>% pull(days) %>% .[[1]]}") +
      transition_manual(week) + ease_aes("linear")