如何在 gganimate 中将自己的图像用于 geom_point?

How to use your own image for geom_point in gganimate?

我正在尝试将我自己的图像用于 geom_point,这是我可以读入的内容。我知道 geom_point 允许您通过简单地书写形状来选择多种形状(超过 300 种) = 243 但我想要自己的图像,例如徽标。

当我没有指定 color = factor(Name) 时,它会按预期工作。当我指定线条的颜色时,图像变成纯色。我希望这条线是彩色的,有什么办法可以解决这个问题吗?谢谢!

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)

这是您要找的吗?

我刚刚将 color = factor(Name) 位置更改为 geom_line 语句。

如果你在第一行使用color = factor(Name)ggplot,它会影响整个情节。所以你在使用这个语句的时候要小心。

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") 

为了方便,我拍了照片。