Search code examples
rimageanimationggplot2gganimate

How to use your own image for geom_point in gganimate?


I am trying to use my own image for geom_point, something I can just read in. I am aware geom_point allows you to choose many shapes (well over 300) by simply writing shape = 243 but I want my own image such as a logo.

When I have not specified color = factor(Name) then it works as expected. When I do specify the colour of the line then the image becomes a solid single colour. I want this line to be coloured so is there any way around this? Thanks!

library(gganimate)
library(gifski)
library(png)
library(ggimage)


Step  <- 1:50
Name  <- rep("A",50)
Image <- rep(c("https://jeroenooms.github.io/images/frink.png"),50)
Value <- runif(50,0,10)
Final <- data.frame(Step, Name, Value, Image)

a <- ggplot(Final, aes(x = Step, y = Value, group = Name, color = factor(Name))) + 
  geom_line(size=1) + 
  geom_image(aes(image=Image)) +
  transition_reveal(Step) + 
  coord_cartesian(clip = 'off') + 
  theme_minimal() +
  theme(plot.margin = margin(5.5, 40, 5.5, 5.5)) +
  theme(legend.position = "none") 

options(gganimate.dev_args = list(width = 7, height = 6, units = 'in', res=100))
animate(a, nframes = 100)

Replacing geom_point with uploaded image


Solution

  • Is this what your are looking for ?

    I Just changed the color = factor(Name) position to geom_line statement.

    If you use color = factor(Name) with ggplot in first row, it will affect to whole plot. So you should take care when using this statement.

    a <- ggplot(Final, aes(x = Step, y = Value, group = Name)) + 
      geom_line(size=1, aes(color = factor(Name))) + 
      geom_image(aes(image=Image)) +
      transition_reveal(Step) + 
      coord_cartesian(clip = 'off') + 
      theme_minimal() +
      theme(plot.margin = margin(5.5, 40, 5.5, 5.5)) +
      theme(legend.position = "none") 
    

    For convenience, i captured the picture .

    enter image description here