Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 362
user3456588 Avatar asked Oct 17 '19 20:10

user3456588


People also ask

What is another way you can save your gganimate plot other than as a GIF input the file extension?

In order to save the animation to a specific location, you can use the anim_save() function which, like ggplot2::ggsave , defaults to taking the last rendered animation and writes it to a file.

How do I save an animated plot in R?

If you need to save the animation for later use you can use the anim_save() function. It works much like ggsave() from ggplot2 and automatically grabs the last rendered animation if you do not specify one directly.


1 Answers

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

like image 132
Steve Lee Avatar answered Oct 13 '22 00:10

Steve Lee