gganimate show rounded values during transition

Hello everybody,

I want to create an animated barplot with the gganimate package. At the top of each bar, I want to put the value of the bar rounded to zero digits.

Consider the following example:

# Example data
df <- data.frame(ordering = c(rep(1:3, 2), 3:1, rep(1:3, 2)),
                 year = factor(sort(rep(2001:2005, 3))),
                 value = round(runif(15, 0, 100)),
                 group = rep(letters[1:3], 5))

# Create animated ggplot
ggp <- ggplot(df, aes(x = ordering, y = value)) +
  geom_bar(stat = "identity", aes(fill = group)) +
  transition_states(year, transition_length = 2, state_length = 0) +
  geom_text(y = df$value, label = as.integer(round(df$value)))
ggp

Unfortunately, I did not manage to round the values properly. Is there a way to round values during transition?

I think the trick is to use format()

  geom_text(mapping=aes(y = value, label = format(round(value))))

Hello nirgrahamuk!
Thank you for you answer! Sadly its not working for me. The tweened numbers during the transition just are not been shown anymore, if i use format()

the values at the "fix points" are already rounded

strange..
Here is my full reprex, then my session info (so you can compare version numbers for the relevant packages, and finally the image result I get.

library(tidyverse)
library(gganimate)

# Example data
df <- data.frame(ordering = c(rep(1:3, 2), 3:1, rep(1:3, 2)),
                 year = factor(sort(rep(2001:2005, 3))),
                 value = round(runif(15, 0, 100)),
                 group = rep(letters[1:3], 5))

# Create animated ggplot
ggp <- ggplot(df, aes(x = ordering, y = value)) +
  geom_bar(stat = "identity", aes(fill = group)) +
  transition_states(year, transition_length = 2, state_length = 0) +
  geom_text(mapping=aes(y = value, label = format(round(value))))
ggp
sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] gganimate_1.0.5 forcats_0.4.0   stringr_1.4.0   dplyr_0.8.5     purrr_0.3.3    
 [6] readr_1.3.1     tidyr_1.0.2     tibble_3.0.0    ggplot2_3.2.1   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] progress_1.2.2    tidyselect_1.0.0  haven_2.2.0       lattice_0.20-38  
 [5] colorspace_1.4-1  vctrs_0.2.4       generics_0.0.2    rlang_0.4.5      
 [9] pillar_1.4.3      glue_1.4.0        withr_2.1.2       DBI_1.1.0        
[13] tweenr_1.0.1      dbplyr_1.4.2      modelr_0.1.6      readxl_1.3.1     
[17] lifecycle_0.2.0   plyr_1.8.5        munsell_0.5.0     gtable_0.3.0     
[21] cellranger_1.1.0  rvest_0.3.5       labeling_0.3      fansi_0.4.1      
[25] broom_0.5.4       Rcpp_1.0.4        scales_1.1.0      backports_1.1.5  
[29] magick_2.3        jsonlite_1.6.1    farver_2.0.3      fs_1.3.1         
[33] digest_0.6.25     hms_0.5.3         stringi_1.4.6     grid_3.6.2       
[37] cli_2.0.2         tools_3.6.2       magrittr_1.5      lazyeval_0.2.2   
[41] crayon_1.3.4      pkgconfig_2.0.3   ellipsis_0.3.0    xml2_1.2.2       
[45] prettyunits_1.1.1 reprex_0.3.0      lubridate_1.7.4   assertthat_0.2.1 
[49] httr_1.4.1        rstudioapi_0.11   R6_2.4.1          nlme_3.1-142     
[53] compiler_3.6.2   

preview1

Thank you a lot for your help and the time spend doing this! I will check all my packages and versions now.

To make sure we are talking about the same problem: my target is to get a number that is counting up each step of the movement of the bar. for example:

when the green bar is rising, it shows in your example the values '12' and '86'. i would like to have it '12','13','14'...'86' like in the first example (without decimals)

my next attempt would be to use tweenr to stretch the data and animate afterwards. what do you think?

according to this there isnt an easy way

You will have to calculate the values to use frame by frame / animate manually (i believe)

With interpolation the values i could fix that problem. But now, the bars are switching the positions extremely fast. Do you have an idea how to solve this? Putting the fps down will, again, un-smooth the number count. i was trying already every combination of

transition_states(datum, transition_length = 25, state_length = 1)

maybe experiment with the animate() function , keep fps high, but for same duration implies higher total frames.

didnt work, because its making both, the animation itself and the numbercount faster.

the trick was to interpolate both axes very fine, after setting a rating for one axis to order the values.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.