Kevin_Nguyen
Kevin_Nguyen

Reputation: 112

Animate bar chart error in last plot (gganimate)?

I created an animated bar chart but the label value is not right. I don't know why cum_2 in my data is an integer, but in the resulting gif the label text appears as a real number (example: 674.5... instead of 675).

library(tidyverse)
library(gganimate)
df <- data.frame(date = c(rep('2020-07-08',4), rep('2020-07-09',4), 
                       rep('2020-07-10',4), rep('2020-07-11',4),
                       rep('2020-07-12',4)),
              sub = rep(c("PYTHON", "SQL", "VBA", "R"),5),
              cum_2 =  c(659,609,454,450,659,609,468,450,670,609,478,450,670, 609,486,461,679,609,486,469),
              rank = rep(c(1,2,3,4),5))

fill <- c('red','blue','green','orange')  # example fill colors

df %>% 
  ggplot(aes(rank, y = cum_2, 
             fill = sub)) +
  geom_col(alpha = 0.6)+
  scale_fill_manual(values = fill) +
  geom_text(aes(y = 0, 
                label = paste(sub, " ")), 
            vjust = 0.2, 
            hjust = 1) +
  transition_states(states = date,transition_length = 4, state_length = 1)  +
  geom_text(aes(y = cum_2,
                label = round(cum_2,0), 
                hjust = 0,
                size = 12)) +
  scale_x_reverse() +
  coord_flip(clip = "off", expand = FALSE) +
  guides(color = FALSE, fill = FALSE) +
  theme(axis.line=element_blank(),
        axis.text.x=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        legend.position="none",
        panel.background=element_blank(),
        panel.border=element_blank(),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        panel.grid.major.x = element_line( size=.1, color="grey" ),
        panel.grid.minor.x = element_line( size=.1, color="grey" ),
        plot.title=element_text(size=25, hjust=0.5, face="bold", colour="grey", vjust=-1),
        plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="grey"),
        plot.caption =element_text(size=8, hjust=0.5, face="italic", color="grey"),
        plot.background=element_blank(),
        plot.margin = margin(2,2, 2, 4, "cm")) +
  labs(title = 'MCI Vietnam : {closest_state}',  
       subtitle  =  "Number of students") +
  view_follow(fixed_x = TRUE) +
  enter_fade() +
  exit_shrink() +
  ease_aes('sine-in-out') -> anim

# gif
animate(anim, nframes = length(unique(df$date)), 
        detail = 5,
        fps = 5,
        # duration = 20,
        renderer = gifski_renderer("file.gif"))

Last plot in gif file here: enter image description here

Thanks all for read & support me.

Upvotes: 0

Views: 215

Answers (1)

chemdork123
chemdork123

Reputation: 13843

Since the y position of the bars is tweened in the animation (cum_2 in your data frame), and the label position and text are based on cum_2, it will also get tweened and the result is that you have numbers with lots of decimal places appearing as a result.

Luckily, the fix is pretty easy: just force your label to be evaluated as.character(). Change your geom_text line to read as follows, and that fixes the issue:

  geom_text(aes(y = cum_2, label = as.character(round(cum_2,0)), 
                hjust = 0, size = 12))

Here's the result (saved with duration=5):

enter image description here

*FYI: your code was missing the colors you used, so here I defined fill=c('red','blue','green','orange').

Upvotes: 2

Related Questions