{closest_state} in gganimate is not working properly for me.

Hey guys. I'm having trouble getting {closest_state} to work the way I need it to. I'm trying to plot data over time by year. I've been able to make the plot.title work with {closest_state} as intended. It updates the plot.title with the year as expected. However, I'm trying to make the year text appear on the bottom right of the plot. Here's what I mean:
https://imgur.com/pY7Tzry
I'm trying to get the year text to appear on the bottom right of the plot where is says 'YEAR'. I've used an annotation_custom for the bottom right text. When I set the annotation label to {closest_state} r annotation_custom(textGrob(label ='{closest_state}', x = 0.75, y = 0.1, hjust = 0, gp = gpar(fontsize = 150))) , it doesn't actually update the text to the year, it just keeps the '{closest_state}' text. Here's what I mean:
https://imgur.com/pDNvIlc
The plot.title is working correctly and updating the year, but the bottom right text is not updating. I just need text in the bottom right of the plot to show the current year. Any help is greatly appreciated. Apologies if this is an obvious question. I'm an R noob. :sweat_smile:

Here's my plot's R code:

library(tidyverse)
library(ggimage)
library(gganimate)
library(gapminder)
library(av)
library(grid)

plotData <- gapminder %>%
  filter(continent == "Americas") %>%
  group_by(year) %>%
  # The * 1 makes it possible to have non-integer ranks while sliding
  mutate(rank = min_rank(-gdpPercap) * 1) %>%
  mutate(height = gdpPercap / max(gdpPercap)) %>%
  ungroup()

animatedPlot <- plotData %>%
  ggplot() +
  geom_col(aes(max(rank) - rank, height, fill = country, alpha = ifelse(max(rank) - rank < 15, 0, 0.7))) +
  geom_text(aes(max(rank) - rank, -0.03, label = country, alpha = ifelse(max(rank) - rank < 15, 0, 0.8)), hjust = 1, size = 15) +
  geom_text(aes(max(rank) - rank, height + 0.1, label = gdpPercap, alpha = ifelse(max(rank) - rank < 15, 0, 0.8)), hjust = 0, size = 15) +
  geom_image(aes(max(rank) - rank, height + 0.05 ), image = "https://www.r-project.org/logo/Rlogo.png") +
  coord_flip() +
  guides(fill = FALSE, alpha = FALSE) +
  labs(title='{closest_state}', x = NULL, y = NULL) +
  annotation_custom(textGrob(label ='{closest_state}', x = 0.75, y = 0.1, hjust = 0, gp = gpar(fontsize = 150))) +
  scale_y_continuous(limits = c(-0.4, 1.5)) +
  theme_void() +
  theme(plot.title = element_text(hjust = 0.5, size = 80)) +
  transition_states(year, transition_length = 200000, state_length = 0, wrap = FALSE) +
  view_follow(fixed_x = c(15, 24), fixed_y = c(-0.3, 1.4))

#Preview at intended size
animate(animatedPlot, width = 1920, height = 1080, duration = 10, fps = 5)

From the ggplot2 docs, it sounds like annotation_custom() is inherently not affected by other ggplot settings:

This is a special geom intended for use as static annotations that are the same in every panel. These annotations will not affect scales (i.e. the x and y axes will not grow to cover the range of the grob, and the grob will not be modified by any ggplot settings or mappings).

What happens if you use annotate()?

Another idea might be to pass {closest_state} to caption as well as title inside of the labs(), and then adjust the positioning of the caption using theme arguments.

2 Likes

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