Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R按时间绘制_R_Ggplot2_Dynamic_Shiny_Gganimate - Fatal编程技术网

R按时间绘制

R按时间绘制,r,ggplot2,dynamic,shiny,gganimate,R,Ggplot2,Dynamic,Shiny,Gganimate,我有一行数据,对应于给定时间(游戏秒)的事件(收据、铲球或持球)。我想有一个动态图,根据每个事件的时间添加它们。我正在尝试的解决方案是将图形放入循环中。但它不起作用,它只显示最后一次迭代 ui <- fluidPage( titlePanel("Event"), sidebarLayout( sidebarPanel( sliderInput("slider", "slider",

我有一行数据,对应于给定时间(游戏秒)的事件(收据、铲球或持球)。我想有一个动态图,根据每个事件的时间添加它们。我正在尝试的解决方案是将图形放入循环中。但它不起作用,它只显示最后一次迭代

    ui <- fluidPage(
  titlePanel("Event"),
  
  sidebarLayout(
    sidebarPanel(
      sliderInput("slider", "slider",
                min = 0, max = 80, value = c(0, 80)),
      actionButton("do", "Play")
    ),
    mainPanel(
      plotOutput("myplot")
    )
  )
)

什么是数据事件?可以用dput()发布示例数据吗?您应该尝试用lappy替换for循环。此外,也许您应该创建一个数据框,而不是键入这么多geom_段(62,103,103,98,44,43,72,72,179),X.Pass.end=c(NA,NA,NA,NA,NA,NA,NA,453,NA),Y.Pass.end=c(NA,NA,NA,NA,NA,179,NA),row.names=c(NA,-9L),class=c(“tbl_-df”,“tbl”,“data.frame”))它是如何创建数据帧的?我希望每个StatName都有一个不同的表示形式。此数据帧中没有id或类型。您确定这是一个可重复性最低的示例吗?很抱歉,我忘记了团队id,我编辑了PosterHaps。最好使用gganimate实现它。示例如下:
    shinyServer(function(input, output) {
  
  play<- reactiveValues(data=0)
  
  ymin <- 0 # minimum width
  ymax <- 70 # maximum width
  xmin <- -20 # minimum length
  xmax <- 120 # maximum length
  padding = 5
  
  # set our colours for our pitch
  grass_colour <- "#538032"
  line_colour <- "#ffffff"
  background_colour <- "#538032"
  goal_colour <- "#ffffff"
  
    
  observeEvent(input$do, {
    play$data<-1
  })
  
  output$myplot<-renderPlot({

    if(play$data==1){
      for(i in input$slider[1]:input$slider[2]){
        #plot from the start of the sequence to the current time
        DataEventF<-DataEvent[DataEvent$GameSeconds>=input$slider[1]&DataEvent$GameSeconds<=i,]
       p<-ggplot(DataEventF) + xlim(c(xmin-padding,xmax+padding)) +
          ylim(c(ymin-padding,ymax+padding)) +
          theme_blankPitch() +
          geom_rect(aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill = NA, colour = line_colour) +
          geom_segment(aes(x = 0, y = ymin, xend =0, yend = ymax),colour = line_colour) + geom_segment(aes(x = 100, y = ymin, xend =100, yend = ymax),colour = line_colour) + geom_segment(aes(x = 50, y = ymin, xend =50, yend = ymax),colour = line_colour) + geom_segment(aes(x = 22, y = ymin, xend = 22, yend = ymax),colour = line_colour)+ geom_segment(aes(x = 40, y = ymin, xend = 40, yend = ymax),colour = line_colour,linetype="dashed")+ geom_segment(aes(x = 60, y = ymin, xend = 60, yend = ymax),colour = line_colour,linetype="dashed")+ geom_segment(aes(x = 78, y = ymin, xend = 78, yend = ymax),colour = line_colour)+ geom_segment(aes(x = 5, y = ymin, xend = 5, yend = ymax),colour = line_colour,linetype="dashed")+ geom_segment(aes(x = 95, y = ymin, xend = 95, yend = ymax),colour = line_colour,linetype="dashed")+geom_segment(aes(x = 0, y = 32.3, xend = 0, yend = 37.7),colour = goal_colour, size = 2)+geom_segment(aes(x = 100, y = 32.3, xend = 100, yend = 37.7),colour = goal_colour, size = 2) +
          geom_segment(aes(x = 19.5, y = 15, xend =24.5, yend = 15),colour = line_colour)+ geom_segment(aes(x = 19.5, y = 55, xend =24.5, yend = 55),colour = line_colour)+ geom_segment(aes(x = 47.5, y = 15, xend =52.5, yend = 15),colour = line_colour)+ geom_segment(aes(x = 47.5, y = 55, xend =52.5, yend = 55),colour = line_colour)  + geom_segment(aes(x = 75.5, y = 15, xend =80.5, yend = 15),colour = line_colour)+ geom_segment(aes(x = 75.5, y = 55, xend =80.5, yend = 55),colour = line_colour) +     
          geom_point(data = DataEventF[DataEventF$StatName=="Receipts",],aes(x = X, y = Y,colour=TeamId,fill=TeamId),
                     pch = 21,
                     size = 2) +
          geom_point(data = DataEventF[DataEventF$StatName=="Made Tackle",],aes(x = X, y = Y,colour=TeamId),
                     pch = 2,
                     size = 2) + 
          geom_segment(data = DataEventF[DataEventF$StatName=="Other Passes",],aes(x=X, y=Y, xend=X.Pass.end, yend=Y.Pass.end, colour=TeamId), size = 1,linetype="dotted") 

    
        plot(p)
      } 
    }
  })
})
structure(list(StatName = c("Receipts", "Made Tackle", "Made Tackle", 
"Receipts", "Receipts", "Made Tackle", "Receipts"), GameSeconds = c(7, 
9, 10, 21, 26, 26, 34), X = c(110, 125, 125, 141, 385, 370, 366
), Y = c(62, 103, 103, 98, 44, 43, 72), X.Pass.end = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), 
    Y.Pass.end = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), TeamId = structure(c(1L, 2L, 2L, 1L, 
    2L, 1L, 2L), .Label = c("1114", "1128"), class = "factor")), row.names = c(NA, 
-7L), class = c("tbl_df", "tbl", "data.frame"))