1

Based on this SO question I am trying to create an animated bar plot, but the bar label tween values are showing hexadecimal values:

library(ggplot2)
library(dplyr)
library(gganimate)

set.seed(123)

df <- data.frame(
  period = rep(1:3, each = 5),
  x = rep(LETTERS[1:5], 3),
  p = rbeta(15, 3, 2)
)

data <- df %>% 
  group_by(period) %>% 
  mutate(rank = min_rank(-p)) %>% 
  ungroup()

ggplot(data, aes(x = rank)) + 
  geom_tile(aes(y = p / 2, 
                height = p,
                width = 0.75)) + 
  geom_text(aes(y = p,
                label = scales::percent(p, 1)),
            hjust = 0,
            nudge_y = 0.005) +
  coord_flip(clip = "off", expand = F) + 
  scale_x_reverse() + 
  labs(title = 'Period {closest_state}',
       x = "",
       y = "") + 
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        plot.margin = margin(r = 5, unit = "cm")) + 
  transition_states(period) + 
  ease_aes('linear')

enter image description here


Things I have tried

  1. As this other SO question suggests I have tried label = round(p * 100).
  2. I have tried to create character labels in df: df$labs <- paste0(round(df$p * 100), "%") and then using them in the geom_text label instead of p.

Questions

I have two questions:

  1. How do I correct these tween values so they properly display as rounded percentages?
  2. Is there a way to hide tween values and only show the true values once a transition state is reached (e.g., at Period 1, Period 2, and Period 3 only)?
LMc
  • 12,577
  • 3
  • 31
  • 43
  • 1
    I am using `gganimate 1.0.7` and it renders for me without that labeling error. For me, the label switches once midway through the transition. – Jon Spring Sep 21 '22 at 19:38
  • 1
    I could reproduce the issue using `gganimate 1.0.8`. (: – stefan Sep 21 '22 at 19:41
  • I am using `1.0.8` but I downgraded to `1.0.7` and still encountered this issue. I am also using `ggplot2 3.3.5`, `dplyr 1.0.7`, and `scales 1.1.1`. – LMc Sep 21 '22 at 22:16
  • @JonSpring can you tell me what version of `gifski` you are running? I am running `1.6.6.1`. – LMc Sep 22 '22 at 17:12
  • Huh, looks like on this computer (a mac) I didn't have gifski installed at all, so I'm guessing it triggers another renderer. I would have expected the error you're seeing would have developed upstream of that. I restarted R, installed gifski 1.6.6-1, re-ran code, and still looks ok. – Jon Spring Sep 22 '22 at 17:21

0 Answers0