问题描述
我在给定时间(GameSeconds)的一行数据对应一个事件(收据,发球或发球)。我想有一个动态图,根据事件的时间添加每个事件。我正在尝试的解决方案是将图形循环放置。但是它不起作用,它只显示最后一次迭代。
ui <- fluidPage(
titlePanel("Event"),sidebarLayout(
sidebarPanel(
sliderInput("slider","slider",min = 0,max = 80,value = c(0,80)),actionButton("do","Play")
),mainPanel(
plotOutput("myplot")
)
)
)
和
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,xend =100,colour = line_colour) + geom_segment(aes(x = 50,xend =50,colour = line_colour) + geom_segment(aes(x = 22,xend = 22,colour = line_colour)+ geom_segment(aes(x = 40,xend = 40,colour = line_colour,linetype="dashed")+ geom_segment(aes(x = 60,xend = 60,linetype="dashed")+ geom_segment(aes(x = 78,xend = 78,colour = line_colour)+ geom_segment(aes(x = 5,xend = 5,linetype="dashed")+ geom_segment(aes(x = 95,xend = 95,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,xend = 100,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,yend = 55),colour = line_colour)+ geom_segment(aes(x = 47.5,xend =52.5,colour = line_colour) + geom_segment(aes(x = 75.5,xend =80.5,colour = line_colour)+ geom_segment(aes(x = 75.5,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",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,size = 1,linetype="dotted")
plot(p)
}
}
})
})
也许这是不可能的,我必须使用gganimate软件包之类的解决方案,但是我也遇到了一些麻烦,因为该软件包的加载时间较长。
谢谢!
样本数据:
structure(list(StatName = c("Receipts","Made Tackle","Receipts","Receipts"),GameSeconds = c(7,9,10,21,26,34),X = c(110,125,141,385,370,366
),Y = c(62,103,98,44,43,72),X.Pass.end = c(NA_real_,NA_real_,NA_real_),Y.Pass.end = c(NA_real_,TeamId = structure(c(1L,2L,1L,2L),.Label = c("1114","1128"),class = "factor")),row.names = c(NA,-7L),class = c("tbl_df","tbl","data.frame"))
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)