【问题标题】:Animated bar plot over time in R [duplicate]R中随时间变化的动画条形图[重复]
【发布时间】:2019-02-28 10:37:27
【问题描述】:

我想在 R 中创建一个动画条形图,显示每个球员和比赛日的进球数。

下面是我创建的虚构数据:

df <- data.frame(player = c("Aguero", "Salah", "Aubameyang", "Kane", "Aguero", "Salah", "Aubameyang", "Kane", "Aguero", "Salah", "Aubameyang", "Kane"), 
             team = c("ManCity", "Liverpool", "Arsenal", "Tottenham", "ManCity", "Liverpool", "Arsenal", "Tottenham", "ManCity", "Liverpool", "Arsenal", "Tottenham"), 
             gameday = c(1,1,1,1,2,2,2,2,3,3,3,3),
             goals = c(0,1,2,0,1,1,3,1,2,1,3,2),
             stringsAsFactors = F)

根据我想创建动画条形图的数据。条形图应在每个比赛日进行动画处理,并在图上方显示最佳得分手。

下面我创建了一个我的想法的简单可视化。

ggplot(data=df, aes(x=reorder(Player, Goals), y=Goals, fill=Team)) +
  geom_bar(stat="identity") +
  theme(legend.position = "none", axis.text.y=element_blank(), 
  axis.title.y=element_blank()) +
  geom_text(aes(label=Player), vjust=1, hjust=-0.1, color="white", size=3.5) +
  coord_flip()

条形图的灵感来自这样一个视频: https://www.youtube.com/watch?v=U8CpdQnWH7Y

是否有可能为此创建动画条形图?

非常感谢您的帮助!

【问题讨论】:

  • 请包含您用于制作图表的代码
  • 是的,很抱歉

标签: r animation ggplot2 plot bar-chart


【解决方案1】:

是的,你有 gganimate 包来为 ggplots 设置动画。您可以通过查找 [r][ggplot2] animate questions 找到它,但由于最重要的答案不是最新的语法,这里有一些代码:

library("ggplot2")
library("gganimate")
ggplot(data=df, aes(x=reorder(Player, Goals), y=Goals, fill=Team)) +
  geom_bar(stat="identity") +
  theme(legend.position = "none", axis.text.y=element_blank(), 
  axis.title.y=element_blank()) +
  geom_text(aes(label=Player), vjust=1, hjust=-0.1, color="white", size=3.5) +
  coord_flip() +
  ## gganimate code
  labs(title = 'Gameday: {frame_time}') +
  transition_time(gameday) +
  ease_aes('linear')

(代码未经测试,但应该可以工作)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 2021-10-04
  • 2015-04-10
  • 2020-10-29
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多