r-如何根据所选事件在ggplot中更改字幕



所以现在我正在从正在进行的篮球比赛中带回数据。随着游戏的进行,我正在显示实况,所以我只显示最喜欢的。我想向大家展示一下在比赛中最受欢迎的球队。我已经筛选出了处于劣势的球队,只剩下最喜欢的球队,但我不知道在我的情节中显示哪支球队是最喜欢的,因为我正在根据赛事名称(比赛名称,两支球队都有(筛选我的折线图。我正在使用shine使其具有互动性,所以我想知道是否可以找到一种方法将我的字幕(目前字幕="游戏演讲"(更改为最喜欢的名称。

数据图片

因此,目前当服务器运行时,我可以根据事件和折线图自动填充在下拉列表中进行选择。我希望字幕能改变,这样它就会显示我最喜欢的(或者我正在看哪支球队的台词(

server = function(input, output, session) {

#load data
df3 = read.csv(paste0("C:/Users/Ethan Altmayer/Desktop/Gambling Data/R Analysis/Consolidated_Data_",Sys.Date()-1,".csv"))

#summarize data
data = reactive({
req(input$Event)
df3 <- filter(df3, Side == "Favorite")
df3 <- df3 %>% filter(Event %in% input$Event)
})

#select list dynamically
observe({
updateSelectInput(session, "Event", choices = df3$Event)
})

#plot
output$plot = renderPlot({
g = ggplot(data(), aes(x=Time.Remaining)) +

geom_line(size = 1, aes(y=Original.Line, color = "#69b3a2")) +
geom_point(color="orchid", size = 2, aes(y=Live.Line)) +
geom_line(size = 1, aes(y=Live.Line, color = "steelblue")) +
labs(title = "Live Line vs Original",
subtitle = "Game Spead",
y = "Line", x = "Time Remaining") +
facet_wrap(~ Half) + 
scale_x_reverse() +
scale_color_discrete(name = "Line Type", labels = c("Original Line", "Live Line")) +
theme(plot.background = element_rect(fill = "Snow"))
print(g)
})

}  
ui = basicPage(
h1("Game"),
selectInput(inputId = "Event", 
label = "Choose Game", "Names"),
plotOutput("plot")
)
shinyApp(ui = ui, server = server)

下一个代码可能很有用:

server = function(input, output, session) {

#load data
df3 = read.csv(paste0("C:/Users/Ethan Altmayer/Desktop/Gambling Data/R Analysis/Consolidated_Data_",Sys.Date()-1,".csv"))

#summarize data
data = reactive({
req(input$Event)
df3 <- filter(df3, Side == "Favorite")
df3 <- df3 %>% filter(Event %in% input$Event)
})

#select list dynamically
observe({
updateSelectInput(session, "Event", choices = df3$Event)
})

#plot
output$plot = renderPlot({
g = ggplot(data(), aes(x=Time.Remaining)) +

geom_line(size = 1, aes(y=Original.Line, color = "#69b3a2")) +
geom_point(color="orchid", size = 2, aes(y=Live.Line)) +
geom_line(size = 1, aes(y=Live.Line, color = "steelblue")) +
labs(title = "Live Line vs Original",
subtitle = unique(data()$Team),
y = "Line", x = "Time Remaining") +
facet_wrap(~ Half) + 
scale_x_reverse() +
scale_color_discrete(name = "Line Type", labels = c("Original Line", "Live Line")) +
theme(plot.background = element_rect(fill = "Snow"))
print(g)
})

}  
ui = basicPage(
h1("Game"),
selectInput(inputId = "Event", 
label = "Choose Game", "Names"),
plotOutput("plot")
)
shinyApp(ui = ui, server = server)

最新更新