This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# คำนวณคะแนนรวมแต่ละทีม
team_points <- df %>%
group_by(constructor_id) %>%
summarise(total_points = sum(points, na.rm = TRUE)) %>%
arrange(desc(total_points))
# สร้างกราฟแท่ง
p1 <- ggplot(team_points, aes(x = reorder(constructor_id, total_points), y = total_points, fill = constructor_id)) +
geom_col(show.legend = FALSE) +
coord_flip() +
labs(title = "คะแนนรวมของแต่ละทีมตลอด 17 สนาม",
x = "ทีม (Constructor)",
y = "คะแนนรวม (Points)") +
theme_minimal() +
geom_text(aes(label = total_points), hjust = -0.1, size = 3.5)
ggplotly(p1)
# ดึงรายชื่อ 5 ทีมท็อปจากคะแนนรวม
top5_teams <- team_points %>% head(5) %>% pull(constructor_id)
top5_df <- df %>% filter(constructor_id %in% top5_teams)
# UI ส่วนเลือกทีมและนักแข่ง
fluidRow(
column(6,
selectInput("team_g2", "1. เลือกทีม (จาก Top 5 ทีม):",
choices = c("ทั้งหมด", top5_teams),
selected = "ทั้งหมด")
),
column(6,
renderUI({
if (input$team_g2 == "ทั้งหมด") {
drivers_list2 <- unique(top5_df$driver_id)
} else {
drivers_list2 <- top5_df %>%
filter(constructor_id == input$team_g2) %>%
pull(driver_id) %>%
unique()
}
selectInput("driver_g2", "2. เลือกผู้เล่น:",
choices = c("ทั้งหมด", drivers_list2),
selected = "ทั้งหมด")
})
)
)
# แสดงผล Scatter Plot โดยตรง
renderPlotly({
req(input$team_g2)
selected_driver <- input$driver_g2
plot_data <- top5_df %>%
mutate(
highlight_status = case_when(
# เมื่อเลือกผู้เล่นเฉพาะคน
!is.null(selected_driver) && selected_driver != "ทั้งหมด" ~
if_else(driver_id == selected_driver, "จุดที่เลือก (สีแดง)", "อื่นๆ (สีเทา)"),
# เมื่อเลือกเฉพาะทีม
input$team_g2 != "ทั้งหมด" ~
if_else(constructor_id == input$team_g2, "จุดที่เลือก (สีแดง)", "อื่นๆ (สีเทา)"),
# ถ้าไม่เลือกอะไรเลย (แสดงเป็นสีฟ้าทั้งหมด)
TRUE ~ "ทั้งหมด (สีฟ้า)"
)
)
color_map <- c(
"ทั้งหมด (สีฟ้า)" = "#1f77b4",
"จุดที่เลือก (สีแดง)" = "#d62728",
"อื่นๆ (สีเทา)" = "#b0b0b0"
)
p2 <- ggplot(plot_data, aes(
x = round,
y = time_sec,
color = highlight_status,
text = paste("สนามที่:", round,
"<br>สนาม:", circuit_name,
"<br>ทีม:", constructor_id,
"<br>นักแข่ง:", driver_id,
"<br>เวลาต่อรอบ:", time_sec, "วินาที")
)) +
geom_point(size = 3.5, alpha = 0.85) +
scale_color_manual(values = color_map) +
scale_x_continuous(breaks = 1:17) +
labs(title = "เวลาต่อรอบที่ดีที่สุดในแต่ละสนาม (Top 5 ทีม)",
x = "สนามการแข่งขัน (Round 1 - 17)",
y = "เวลาต่อรอบที่ดีที่สุด (วินาที)",
color = "สถานะ") +
theme_minimal()
ggplotly(p2, tooltip = "text")
})
all_teams <- unique(df$constructor_id)
# UI สำหรับเลือกทีมและนักแข่ง (11 ทีม)
fluidRow(
column(6,
selectInput("team_g3", "เลือกทีม (ทั้งหมด 11 ทีม):",
choices = c("ทั้งหมด", all_teams),
selected = "ทั้งหมด")
),
column(6,
renderUI({
if (input$team_g3 == "ทั้งหมด") {
drivers_list3 <- unique(df$driver_id)
} else {
drivers_list3 <- df %>%
filter(constructor_id == input$team_g3) %>%
pull(driver_id) %>%
unique()
}
selectInput("driver_g3", "เลือกผู้เล่น:",
choices = c("ทั้งหมด", drivers_list3),
selected = "ทั้งหมด")
})
)
)
# การ์ดสถิติ 3 ค่าหลัก
renderUI({
filtered_df <- df
if (input$team_g3 != "ทั้งหมด") {
filtered_df <- filtered_df %>% filter(constructor_id == input$team_g3)
}
if (!is.null(input$driver_g3) && input$driver_g3 != "ทั้งหมด") {
filtered_df <- filtered_df %>% filter(driver_id == input$driver_g3)
}
total_pts <- sum(filtered_df$points, na.rm = TRUE)
best_pos <- min(filtered_df$position, na.rm = TRUE)
best_time <- suppressWarnings(min(filtered_df$time_sec, na.rm = TRUE))
best_time_txt <- if(is.infinite(best_time)) "ไม่มีข้อมูล" else paste(best_time, "วินาที")
fluidRow(
column(4,
div(style = "background-color: #eef6ff; border-radius: 8px; padding: 15px; text-align: center; border-left: 5px solid #007bff;",
h4("คะแนนรวม 17 สนาม"),
h2(style = "color: #007bff; font-weight: bold; margin: 0;", total_pts)
)
),
column(4,
div(style = "background-color: #eef9f1; border-radius: 8px; padding: 15px; text-align: center; border-left: 5px solid #28a745;",
h4("Position ที่ดีที่สุด"),
h2(style = "color: #28a745; font-weight: bold; margin: 0;", paste("อันดับที่", best_pos))
)
),
column(4,
div(style = "background-color: #fdf0f0; border-radius: 8px; padding: 15px; text-align: center; border-left: 5px solid #dc3545;",
h4("เวลาเร็วที่สุดใน 17 สนาม"),
h2(style = "color: #dc3545; font-weight: bold; margin: 0;", best_time_txt)
)
)
)
})
br()
# กราฟแสดงอันดับการเข้าเส้นชัย
renderPlotly({
filtered_df <- df
if (input$team_g3 != "ทั้งหมด") {
filtered_df <- filtered_df %>% filter(constructor_id == input$team_g3)
}
if (!is.null(input$driver_g3) && input$driver_g3 != "ทั้งหมด") {
filtered_df <- filtered_df %>% filter(driver_id == input$driver_g3)
}
p3 <- ggplot(filtered_df, aes(x = round, y = position, color = driver_id, group = driver_id,
text = paste("สนามที่:", round,
"<br>นักแข่ง:", driver_id,
"<br>อันดับเข้าเส้นชัย:", position,
"<br>คะแนนสนามนี้:", points))) +
geom_line(alpha = 0.7, size = 1) +
geom_point(size = 2.5) +
scale_y_reverse(breaks = 1:22) +
scale_x_continuous(breaks = 1:17) +
labs(title = "แนวโน้มอันดับการเข้าเส้นชัยในแต่ละสนาม (Finishing Position)",
x = "สนามการแข่งขัน (Round)",
y = "อันดับการเข้าเส้นชัย (Position)",
color = "นักแข่ง") +
theme_minimal()
ggplotly(p3, tooltip = "text")
})