Search code examples
rgganimateggpubr

Ghost bars in gghistogram + gganimate


I am trying to make an animated histogram with the following code:

library(ggpubr)
library(gganimate)

p <- gghistogram(df, x = "Idade", bins = 30,
   add = "mean", rug = T,
   color = "Género", fill = "Género",
   palette = c("#0073C2FF", "#FC4E07", "grey"), 
   xlab ="Idade (anos)", ylab = "(n)", 
   title = "Distribuição de Idades") + 
  theme(legend.position = "right")+ 
  theme_ipsum_rc(grid="Y",base_size = 16,axis_title_size = 14)+
  transition_states(Idade, wrap = F) + shadow_mark() + enter_grow() +  exit_fade() 

animate(p,  nframes=100, fps = 10, renderer = gifski_renderer(loop = FALSE))

but I am getting different darker blue and red bars (ghosts) like this:

Gosth histrogram bars in gganimante

I can't figure it out. Any ideas on how to remove the darker bars? Thanks!


Solution

  • For a given value of x and genero, you have multiple values of y in your dataframe df. The darkshade represents the lower value and lighter shade represents the higher value. If you have two values, you will see two shades, for 3 values you will see 3 shades, and so on. You can subset the dataframe df for a single value of that variable resulting in unique combination of x, y and genero. Then you will get only one shade. Without a sample dataframe, it cannot be confirmed.