r - 参数意味着 R 中不同的行数 - 特定情况

标签 r ggplot2 gganimate

我在以下链接中有两个数据文件:tuckdftgames .

我还有以下代码来使用这些文件创建动画:

library('dplyr')
library('tidyverse')
library('ggplot2')
library('gganimate')
library('ggforce')
library('gifski')
library('readr')
library('deldir')
library('transformr')

colorDict <- c('ARI'='#97233F','ATL'='#A71930','BAL'='#241773','BUF'='#00338D','CAR'='#0085CA','CHI'='#00143F',
               'CIN'='#FB4F14','CLE'='#FB4F14','DAL'='#B0B7BC','DEN'='#002244','DET'='#046EB4','GB'='#24423C',
               'HOU'='#C9243F','IND'='#003D79','JAX'='#136677','KC'='#CA2430','LA'='#002147','LAC'='#2072BA',
               'LV'='#C4C9CC','MIA'='#0091A0','MIN'='#4F2E84','NE'='#0A2342','NO'='#A08A58','NYG'='#192E6C',
               'NYJ'='#203731','PHI'='#014A53','PIT'='#FFC20E','SEA'='#7AC142','SF'='#C9243F','TB'='#D40909',
               'TEN'='#4095D1','WAS'='#FFC20F', 'FBL'='#964B00')

tgames <- read_csv("PATH_TO_TGAMES_DATA")
tuckdf <- read_csv("PATH_TO_TUCKDF_DATA", 
                    col_types = cols(time = col_character())
)

vertical_lines <- c(10,20,30,40,50,60,70,80,90,100,110)
txt = c(10,20,30,40,50,40,30,20,10)
nums = c(20,30,40,50,60,70,80,90, 100)
bot = c(5,5,5,5,5,5,5,5,5)
top = c(48.3, 48.3, 48.3, 48.3, 48.3, 48.3, 48.3, 48.3, 48.3)
bot_nums = data.frame(nums, txt, bot)
top_nums = data.frame(nums, txt, top)
rects <- data.frame(x1 = 0, x2 = 10, x3 = 110, x4 = 120, y1 = 0, y2 = 53.3)

animar <- ggplot(tuckdf, aes(x, y, colour=t_abbr)) +
  #fill="#78b833"
          theme(panel.background = element_rect(fill="#78b833"),
              panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(), legend.position = "bottom",
              axis.ticks = element_blank(), axis.text = element_blank()) +
          geom_rect(data = rects, aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2), inherit.aes = FALSE, 
                    fill = colorDict[[tgames$homeTeamAbbr[[1]]]]) +
          geom_rect(data = rects, aes(xmin = x3, xmax = x4, ymin = y1, ymax = y2), inherit.aes = FALSE,
                    fill = colorDict[[tgames$homeTeamAbbr[[1]]]]) +
          geom_vline(xintercept = vertical_lines, colour="#FFFFFF") +
          geom_text(data = bot_nums, aes(nums, bot, label=txt), inherit.aes = FALSE, color = '#FFFFFF') +
          geom_text(data = top_nums, aes(nums, top, label=txt), inherit.aes = FALSE, color = '#FFFFFF', angle=180) +
          # home team velocities
          # geom_segment(
            #p_data = specdf %>% filter(team == 'home'),
            #mapping = aes(x = x, y = y, xend = x*sin(o), yend = y*cos(o)),
            #colour = colorDict[[tgames$homeTeamAbbr[[1]]]], size = 1, arrow = arrow(length = unit(0.01, "npc"))
          #) + 
          geom_point(aes(), show.legend= FALSE, size = 6) +
          geom_point(shape=1, size = 6, colour='black') +
          geom_text(aes(x, y, label = jerseyNumber), color = '#FFFFFF', size=3) +
          #{if(aes(jerseyNumber) == 22)geom_line()} +
          geom_segment(
            mapping = aes(x = x, y = y, xend = x + 0.5*s*sin(o*pi/180), yend = y + 0.5*s*cos(o*pi/180)),
            show.legend= FALSE, 
            size = 1, arrow = arrow(length = unit(0.01, "npc"))
          ) + 
          scale_colour_manual(values=colorDict) +
          scale_fill_manual(values=colorDict) +
          scale_x_continuous(limits=c(0,120), expand=c(0,0)) +
          scale_y_continuous(limits=c(0,53.3), expand=c(0,0)) + 
          coord_fixed() +
          labs(title="", x="", y="") +
          transition_states(frameId, transition_length = 2, state_length = 1, wrap = FALSE)

animate(animar, renderer = gifski_renderer())
anim_save("C:\\Users\\jakes\\Downloads\\thing3.gif")

此代码产生以下错误:

Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 209, 100

看了数据我还是不太明白这是怎么回事。有人有解决办法吗?我觉得这需要一个非常简单的解决方案,但我找不到它。例如,将 tuckdf 数据帧减少为 frameId 为 <= 30 的数据帧可以消除此错误。不幸的是,我需要所有数据行。

最佳答案

好吧,我认为问题出在默认帧数(100)


  animate(animar, renderer = gifski_renderer(),nframes=210)

关于r - 参数意味着 R 中不同的行数 - 特定情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69761927/

相关文章:

roundrectGrob 相当于 ggplot 中的 geom_rect

r - 如何使用 R 和 ggplot 绘制逻辑回归模型的结果

在 gganimate 中将动画 barplot 条上的标签舍入?

r - 控制动画的速度

r - 在 R markdown 中隐藏评论

R - 根据两列识别重复项,查找值,并删除具有特定值的案例

r - 存储没有日期但不作为类字符的时间

r - 如果使用变量,为什么过滤器函数在 R 中不起作用?

r - ggplot2 - 添加 alpha 参数使 PDF 中的所有其他文本变暗/加粗

r - gganimate 无法正确调用 ImageMagick,可能是路径错误