r - 使用 transition_time 随时间 gganimate,其中时间变量是 NBA 比赛时钟

标签 r ggplot2 gganimate

我正在尝试在单场 NBA 比赛中制作球员和篮球运动的动画。在 NBA 中,比赛时钟从 12 分钟开始减少到 11:59 分钟、11:58、11:57 分钟等。因此,一次控球的数据集有一个从 718.80 开始的 game_clock 变量秒(11.98 分钟)。这是我到目前为止所做的:

gganimate代码:

first_poss_so <- read_csv("https://raw.githubusercontent.com/jasonbaik94/stackoverflow-data/master/first_poss_so.csv")


fullcourt() +
  geom_point(data = first_poss_so, aes(x = h1_x, y = h1_y, group = possID), size=3, color = "blue") +  
  geom_point(data = first_poss_so, aes(x = h2_x, y = h2_y, group = possID), size=3, color = "blue") +  
  geom_point(data = first_poss_so, aes(x = h3_x, y = h3_y, group = possID), size=3, color = "blue") +  
  geom_point(data = first_poss_so, aes(x = h4_x, y = h4_y, group = possID), size=3, color = "blue") +  
  geom_point(data = first_poss_so, aes(x = h5_x, y = h5_y, group = possID), size=3, color = "blue") +  
  geom_point(data = first_poss_so, aes(x = a1_x, y = a1_y, group = possID), size=3, color = "red") +  
  geom_point(data = first_poss_so, aes(x = a2_x, y = a2_y, group = possID), size=3, color = "red") +  
  geom_point(data = first_poss_so, aes(x = a3_x, y = a3_y, group = possID), size=3, color = "red") +  
  geom_point(data = first_poss_so, aes(x = a4_x, y = a4_y, group = possID), size=3, color = "red") +  
  geom_point(data = first_poss_so, aes(x = a5_x, y = a5_y, group = possID), size=3, color = "red") +  
  geom_point(data = first_poss_so, aes(x = x, y = y, group = possID), size=3, color = "gold") +
  transition_time(time = game_clock)

这是full_court()

library(ggplot2)

fullcourt <- function () {

  palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
            "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))

  #Generate Data for the 3 point line
  # Define the circle; add a point at the center if the 'pie slice' if the shape is to be filled
  circleFun <- function(center=c(0,5.25), diameter=20.9, npoints=20000, start=0, end=1, filled=TRUE){
    tt <- seq(start*pi, end*pi, length.out=npoints)
    df <- data.frame(
      y = center[1] + diameter / 2 * cos(tt),
      x = center[2] + diameter / 2 * sin(tt)
    )
    return(df)
  }

  halfCircle <- circleFun(c(0, 5.25), 20.9*2, start=0, end=1, filled=FALSE) 
  ggplot(data=data.frame(y=1,x=1),aes(x,y))+
    ###halfcourt line:
    geom_path(data=data.frame(x=c(47,47),y=c(0,50)))+
    ###outside boy:
    geom_path(data=data.frame(y=c(0,0,50,50,0),x=c(0,94,94,0,0)))+
    ###solid FT semicircle above FT line:
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(19+sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x))+
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(75+sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x))+
    ###dashed FT semicircle below FT line:
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(19-sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x),linetype='dashed')+
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(75-sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x),linetype='dashed')+
    ###kex:
    geom_path(data=data.frame(y=c(17,17,33,33,17),x=c(0,19,19,0,0)))+
    geom_path(data=data.frame(y=c(17,17,33,33,17),x=c(94,75,75,94,94)))+
    ###boy inside the kex:
    geom_path(data=data.frame(y=c(19,19,31,31,19),x=c(0,19,19,0,0)))+
    geom_path(data=data.frame(y=c(19,19,31,31,19),x=c(94,75,75,94,94)))+
    ###restricted area semicircle:
    geom_path(data=data.frame(y=c((-4000:(-1)/1000)+25,(1:4000/1000)+25),x=c(5.25+sqrt(4^2-c(-4000:(-1)/1000,1:4000/1000)^2))),aes(y=y,x=x))+
    geom_path(data=data.frame(y=c((-4000:(-1)/1000)+25,(1:4000/1000)+25),x=c(88.75-sqrt(4^2-c(-4000:(-1)/1000,1:4000/1000)^2))),aes(y=y,x=x))+
    ###halfcourt semicircle:
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(47-sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x))+
    geom_path(data=data.frame(y=c((-6000:(-1)/1000)+25,(1:6000/1000)+25),x=c(47+sqrt(6^2-c(-6000:(-1)/1000,1:6000/1000)^2))),aes(y=y,x=x))+
    ###rim:
    geom_path(data=data.frame(y=c((-750:(-1)/1000)+25,(1:750/1000)+25,(750:1/1000)+25,(-1:-750/1000)+25),x=c(c(5.25+sqrt(0.75^2-c(-750:(-1)/1000,1:750/1000)^2)),c(5.25-sqrt(0.75^2-c(750:1/1000,-1:-750/1000)^2)))),aes(y=y,x=x))+
    geom_path(data=data.frame(y=c((-750:(-1)/1000)+25,(1:750/1000)+25,(750:1/1000)+25,(-1:-750/1000)+25),x=c(c(88.75+sqrt(0.75^2-c(-750:(-1)/1000,1:750/1000)^2)),c(88.75-sqrt(0.75^2-c(750:1/1000,-1:-750/1000)^2)))),aes(y=y,x=x))+
    ###backboard:
    geom_path(data=data.frame(y=c(22,28),x=c(4,4)),lineend='butt')+
    geom_path(data=data.frame(y=c(22,28),x=c(90,90)),lineend='butt')+
    ###three-point line:
    # geom_path(data=data.frame(y=c(-21,-21,-21000:(-1)/1000,1:21000/1000,21,21),x=c(0,169/12,5.25+sqrt(23.75^2-c(-21000:(-1)/1000,1:21000/1000)^2),169/12,0)),aes(y=y,x=x))+
    ###fiy aspect ratio to 1:1
    geom_path(data=halfCircle,aes(x=x,y=y+25))+
    ###Complete the three-point line 
    geom_path(data=data.frame(y=c(4.1,4.1,45.9,45.9),x=c(5.25,0,0,5.25)))+
    geom_path(data=halfCircle,aes(x=94-x,y=y+25))+
    geom_path(data=data.frame(y=c(4.1,4.1,45.9,45.9),x=c(88.75,94,94,88.75)))+
    coord_fixed()+

    ###Clean up the Court 
    theme_bw()+theme(panel.grid=element_blank(), legend.title=element_blank(), panel.border=element_blank(),axis.text=element_blank(),axis.ticks=element_blank(),axis.title=element_blank(),legend.position="top")}

我的 gganimate 代码的问题是 transition_time 向后呈现游戏...因为 game_clock 从大约 12 分钟开始并减少.当然,我可以从 game_clock 中减去 12 分钟,但这会阻止我使用 game_clock 作为明智的标签:ggtitle("Game Clock: {frame_time}")

enter image description here

基本上,我怎样才能让 transition_time 以递减顺序呈现时间? (从12分钟开始到0分钟结束)

最佳答案

(作为惯例,我不会下载我不知道的外部链接数据,所以我将使用标准数据的方法来回答。)

这应该可以通过反转 transition_time 变量的符号,并再次反转标题中的符号来实现,如下所示:

library(gapminder)
library(gganimate)
library(dplyr)

a <- gapminder %>%
  ggplot(aes(gdpPercap, lifeExp, color = continent, group = country)) +
  geom_point() +
  transition_time(-year) +
  labs(title = "Year: {-frame_time}")

animate(a, nframes = 50, duration = 5)

enter image description here

关于r - 使用 transition_time 随时间 gganimate,其中时间变量是 NBA 比赛时钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55502943/

相关文章:

r - 如何在 R Markdown 中选择特定选项卡?

r - R 中整洁/高效的函数编写(垃圾收集)

r - 以 R 中的元素数量为条件删除列表

r - 如何突出显示情节上的时间范围?

r - 如何在ggplot中将手动色标传递给geom_smooth?

r - 比较多个向量

推荐13种或以上类别的比例尺颜色

r - 如何使用 R Markdown 在 html 输出中创建动画

R ggplot2 和 gganimate : animation changes at end

r - ggsave 和 gganimate 中符号的大小一致 'animate'