plot1_data <- read.csv("plot1_data.csv")
plot1_data$ranking_date <- as.Date(plot1_data$ranking_date, format = "%Y-%m-%d")
ui <- fluidPage(
titlePanel("ATP Player Rankings Over Time"),
fluidRow(
column(
width = 3,
wellPanel(
dateRangeInput("date_range", "Select Date Range:",
start = min(plot1_data$ranking_date),
end = max(plot1_data$ranking_date),
min = min(plot1_data$ranking_date),
max = max(plot1_data$ranking_date)),
selectInput("players", "Select Players:",
choices = unique(plot1_data$player),
selected = unique(plot1_data$player),
multiple = TRUE),
checkboxInput("facet", "Facet by Player", value = FALSE)
)
),
column(
width = 9,
plotlyOutput("rankingPlot", height = "700px")
)
)
)
server <- function(input, output) {
filtered_data <- reactive({
plot1_data %>%
filter(player %in% input$players,
ranking_date >= input$date_range[1],
ranking_date <= input$date_range[2])
})
output$rankingPlot <- renderPlotly({
p <- ggplot(filtered_data(), aes(x = ranking_date, y = rank,
text = paste("Player:", player,
"<br>Rank:", rank,
"<br>Date:", ranking_date))) +
geom_line(aes(color = player, group = player), size = 0.5, show.legend = FALSE) +
geom_point(aes(color = player), show.legend = FALSE) +
labs(x = "Year", y = "Rank") +
theme_light() +
scale_y_reverse(limits = c(NA, 1)) +
scale_x_date(breaks = seq(as.Date("1990-01-01"), as.Date("2025-01-01"), by = "5 years"),
labels = scales::date_format("%Y")) +
theme(plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45),
axis.title.x = element_text(margin = margin(t = 15)))
if (input$facet) {
p <- p + facet_wrap(~ player, nrow = 2)
}
ggplotly(p, tooltip = "text")
})
}
shinyApp(ui = ui, server = server)
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.