手动用图像替换数据点

时间:2019-03-04 14:02:13

标签: r ggplot2 gganimate

我用ggplot2和gganimate创建了一个动画情节。在对情节进行动画处理后,我旨在用图像替换数据点。对于该示例,每个球员的形象都应该不同。

# required R packages 
library(tidyr)
library(ggplot2)
library(gganimate)
library(png)

# data to reproduce the results
df <- data.frame(Player = rep(c("Aguero", "Salah", "Aubameyang", "Kane"),6),
             Team = rep(c("ManCity", "Liverpool", "Arsenal", "Tottenham"),6), 
             Gameday = c(rep(c(1),4),rep(c(2),4),rep(c(3),4),rep(c(4),4),rep(c(5),4),rep(c(6),4)),
             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)

p <- ggplot(df, aes(Gameday, Goals, group = Player)) + 
  geom_line(aes(colour=Player)) + 
  geom_segment(aes(xend = Gameday, yend = Goals), linetype = 3) + 
  geom_point(size = 2) + 
  geom_text(aes(x = Gameday, label = Player, colour=Player), hjust = -0.05, vjust=0) + 
  transition_reveal(Gameday) + 
  coord_cartesian(clip = 'off') + 
  labs(title = 'Top Goal Scorers', y = 'Goals scored', x = 'Gameday') + 
  theme_minimal() + 
  theme(legend.position='none')

p + anim_save("goals.gif", animation = last_animation())

运行提供的代码,我将得到以下结果。

enter image description here

如上所述,我的目标是用图像替换数据点。下面,我对该图进行了可视化显示:

enter image description here

我已经检查了以下建议,但没有找到正确的解决方案:https://cran.r-project.org/web/packages/ggimage/vignettes/ggimage.html

0 个答案:

没有答案