title: Including images on axis label in an animated ggplot2
body: <p>I created an animated bar plot displaying goals scored by players (fictional). </p>
  
  <p>Please see the reproduced data for the example: </p>
  
  <pre><code>df &lt;- data.frame(Player = rep(c("Aguero", "Salah", "Aubameyang", "Kane"), 6),
                   Team = rep(c("ManCity", "Liverpool", "Arsenal", "Tottenham"), 6), 
                   Gameday = c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6),
                   Goals = c(0,1,2,0,1,1,3,1,2,1,3,2,2,2,4,3,3,2,4,5,5,3,5,6),
                   stringsAsFactors = F)
  </code></pre>
  
  <p>Following animated bar plot are created by the code below.</p>
  
  <p><a href="https://i.stack.imgur.com/Ryc9v.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/Ryc9v.gif" alt="enter image description here"></a></p>
  
  <pre><code># loading required
  library(tidyverse)
  library(gganimate)
  library(png)
  </code></pre>
  
  <p><em>Edited:</em>
  I would like to include following icons for each player:</p>
  
  <pre><code>icon1.png &lt;- image_read('https://raw.githubusercontent.com/sialbi/examples/master/player1.png')
  icon2.png &lt;- image_read('https://raw.githubusercontent.com/sialbi/examples/master/player2.png')
  icon3.png &lt;- image_read('https://raw.githubusercontent.com/sialbi/examples/master/player3.png')
  icon4.png &lt;- image_read('https://raw.githubusercontent.com/sialbi/examples/master/player4.png')
  
  gap &lt;- df %&gt;%
    group_by(Gameday) %&gt;%
    mutate(rank = min_rank(-Goals) * 1,
           Value_rel = Goals/Goals[rank==1],
           Value_lbl = paste0(" ", Goals)) %&gt;%
    filter(rank &lt;=10) %&gt;%
    ungroup()
  
  gap %&gt;%
    group_by(Player) %&gt;%
    arrange(Gameday) %&gt;%
    mutate(prev.rank = lag(rank)) %&gt;%
    ungroup() %&gt;%
  
    group_by(Gameday) %&gt;%
    arrange(rank, prev.rank) %&gt;%
    mutate(x = seq(1, n())) %&gt;%
    ungroup() %&gt;%
  
    ggplot(aes(x = x, y = Goals, fill = Player, color = Player)) +
    geom_col() +
    geom_text(aes(y = 0, label = Player), size = 5, color="black", hjust = -0.05) +
    geom_text(aes(label = Value_lbl), hjust = 0) +
    coord_flip(clip = "off", expand = FALSE) +
    scale_y_continuous(labels = scales::comma) +
    scale_x_reverse() +
    guides(color = FALSE, fill = FALSE) +
    labs(title = "Gameday: {closest_state}", x="", y = "Goals scored") +
    theme(plot.title = element_text(hjust = 0, size = 26),
          axis.ticks.y = element_blank(), 
          axis.text.y  = element_blank(),
          plot.margin = margin(1,1,1,4, "cm")) +
   transition_states(Gameday, transition_length = 4, state_length = 1) +
   ease_aes('cubic-in-out')
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>To complete the animation I would like to include a picture of each player on the y axis. Below I edited the animation to display the desired result (the circles were chosen to avoid violating any copyrights).</p>
  
  <p>The images (circles) should also move up and down as the bars.</p>
  
  <p>Is there are way to include images on the y-axis?</p>
  
  <p><a href="https://i.stack.imgur.com/aQaPe.png" rel="noreferrer"><img src="https://i.stack.imgur.com/aQaPe.png" alt="enter image description here"></a></p>
  
  <h1>Edited Code</h1>
  
  <p>After the presented suggestions I was able to fix the problems. The code below is working accordingly.</p>
  
  <pre><code>library(imager)
  library(ggimage)
  library(magick)
  library(tidyverse)
  library(gganimate)
  library(png)
  library(gapminder)
  
  
   #read data
   df &lt;- data.frame(Player = rep(c("Aguero", "Salah", "Aubameyang", "Kane"), 6),
               Team = rep(c("ManCity", "Liverpool", "Arsenal", "Tottenham"), 6), 
               Gameday = c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6),
               Goals = c(0,1,2,0,1,1,3,1,2,1,3,2,2,2,4,3,3,2,4,5,5,3,5,6),
               stringsAsFactors = F)
  
  # import images
  df2 &lt;- data.frame(Player = c("Aguero", "Salah", "Aubameyang", "Kane"),
                Image = sample(c("https://raw.githubusercontent.com/sialbi/examples/master/player1.png",
                          "https://raw.githubusercontent.com/sialbi/examples/master/player2.png",
                          "https://raw.githubusercontent.com/sialbi/examples/master/player3.png",
                          "https://raw.githubusercontent.com/sialbi/examples/master/player4.png")),
                stringsAsFactors = F)
  
  
  gap &lt;- df %&gt;%
    group_by(Gameday) %&gt;%
    mutate(rank = min_rank(-Goals) * 1,
           Value_rel = Goals/Goals[rank==1],
           Value_lbl = paste0(" ", Goals)) %&gt;%
    filter(rank &lt;=10) %&gt;%
    ungroup()
  
  p = gap %&gt;%
    left_join(df2, by = "Player") %&gt;% # add image file location to the dataframe being
    group_by(Player) %&gt;%
    arrange(Gameday) %&gt;%
    mutate(prev.rank = lag(rank)) %&gt;%
    ungroup() %&gt;%      
    group_by(Gameday) %&gt;%
    arrange(rank, prev.rank) %&gt;%
    mutate(x = seq(1, n())) %&gt;%
    ungroup()
  
  ggplot(p, aes(x = x, y = Goals, fill = Player, color = Player)) +
    geom_col() +
    geom_text(aes(y = 0, label = Player), size = 5, color="black", hjust = -0.05) +
    geom_text(aes(label = Value_lbl), hjust = 0) +
  
    # where the error occurs 
    geom_image(aes(x = x, Image = Image), y = 0,  
               size = 0.25, hjust = 1,
               inherit.aes = FALSE) +
  
    coord_flip(clip = "off", expand = FALSE) +
    scale_y_continuous(labels = scales::comma) +
    scale_x_reverse() +
    guides(color = FALSE, fill = FALSE) +
    labs(title = "Gameday: {closest_state}", x = "", y = "Goals scored") +
    theme_classic() +
    theme(plot.title = element_text(hjust = 0, size = 26),
          axis.ticks.y = element_blank(),
          axis.text.y  = element_blank(),
          plot.margin = margin(1, 1, 1, 4, "cm")) +
    transition_states(Gameday, transition_length = 4, state_length = 1) +
    ease_aes('cubic-in-out')
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/iUpof.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/iUpof.gif" alt="enter image description here"></a></p>