Test

# Load necessary libraries
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(janitor)
Warning: package 'janitor' was built under R version 4.3.3

Attaching package: 'janitor'

The following objects are masked from 'package:stats':

    chisq.test, fisher.test
library(Lahman)
library(ggplot2)
library(forcats)
library(dplyr)
library(readr)
library(kableExtra)

Attaching package: 'kableExtra'

The following object is masked from 'package:dplyr':

    group_rows
library(yaml)
library(knitr)
Test_again <- read_csv("Test again.csv")
Rows: 83 Columns: 35
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (24): Category, TA Kickout Location, Half, TB Kickout Location, Quarter...
dbl   (1): N#
num   (2): X, Y
lgl   (4): All, TB Origin of Scores, EMPTY CAT, Ungroup
time  (4): Start, Click, End, Duration

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(Test_again)
library(showtext)
Warning: package 'showtext' was built under R version 4.3.3
Loading required package: sysfonts
Warning: package 'sysfonts' was built under R version 4.3.3
Loading required package: showtextdb
Warning: package 'showtextdb' was built under R version 4.3.3
draw_gaa_pitch_portrait <- function() {
  # Define pitch dimensions
  pitch_length <- 145  # Maximum length in meters
  pitch_width <- 90    # Maximum width in meters
  
  # Define six-yard box dimensions
  six_yard_box_length <- 14  # Length (x-direction)
  six_yard_box_width <- 4.5  # Width (y-direction)

  # Create base pitch
  pitch <- ggplot() +
    # Pitch outline
    geom_rect(aes(xmin = 0, xmax = pitch_width, ymin = 0, ymax = pitch_length), 
              fill = "slateblue4", color = "white", size = 1) +
    # Six-yard boxes
    geom_rect(aes(xmin = (pitch_width - six_yard_box_length) / 2, xmax = (pitch_width + six_yard_box_length) / 2, 
                  ymin = 0, ymax = six_yard_box_width), 
              fill = NA, color = "white", size = 1) +  # Bottom six-yard box
    geom_rect(aes(xmin = (pitch_width - six_yard_box_length) / 2, xmax = (pitch_width + six_yard_box_length) / 2, 
                  ymin = pitch_length - six_yard_box_width, ymax = pitch_length), 
              fill = NA, color = "white", size = 1) +  # Top six-yard box
    # Goal areas
    geom_rect(aes(xmin = 35, xmax = 55, ymin = 0, ymax = 14), fill = NA, color = "white", size = 1) +
    geom_rect(aes(xmin = 35, xmax = 55, ymin = pitch_length - 14, ymax = pitch_length), fill = NA, color = "white", size = 1) +
    # Halfway line
    geom_segment(aes(x = 0, xend = pitch_width, y = pitch_length / 2, yend = pitch_length / 2), 
                 color = "white", size = 1, linetype = "dashed") +
    # 13m, 21m, 45m, 65m lines
    geom_segment(aes(x = 0, xend = pitch_width, y = 14, yend = 14), color = "white", size = 1) +  # Bottom 13m line
    geom_segment(aes(x = 0, xend = pitch_width, y = pitch_length - 14, yend = pitch_length - 14), color = "white", size = 1) +  # Top 13m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 21, yend = 21), color = "white", size = 1) +  # Bottom 21m line
    geom_segment(aes(x = 0, xend = pitch_width, y = pitch_length - 21, yend = pitch_length - 21), color = "white", size = 1) +  # Top 21m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 45, yend = 45), color = "white", size = 1) +  # Bottom 45m line
    geom_segment(aes(x = 0, xend = pitch_width, y = pitch_length - 45, yend = pitch_length - 45), color = "white", size = 1) +  # Top 45m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 65, yend = 65), color = "white", size = 1) +  # Bottom 65m line
    geom_segment(aes(x = 0, xend = pitch_width, y = pitch_length - 65, yend = pitch_length - 65), color = "white", size = 1) +  # Top 65m line
    # Existing arcs (x = 45)
    annotate("path", x = 45 + 15 * cos(seq(pi, 0, length.out = 100)), 
             y = 21 + 13 * sin(seq(pi, 0, length.out = 100)), color = "white", size = 1) +  # Bottom arc
    annotate("path", x = 45 + 15 * cos(seq(pi, 2 * pi, length.out = 100)), 
             y = pitch_length - 21 + 13 * sin(seq(pi, 2 * pi, length.out = 100)), color = "white", size = 1) +  # Top arc
    # New 40-meter arcs
    annotate("path", x = 45 + 40 * cos(seq(pi, 0, length.out = 100)), 
             y = 21 + 19 * sin(seq(pi, 0, length.out = 100)), color = "white", size = 1) +  # Bottom 40m arc
    annotate("path", x = 45 + 40 * cos(seq(pi, 2 * pi, length.out = 100)), 
             y = pitch_length - 21 + 19 * sin(seq(pi, 2 * pi, length.out = 100)), color = "white", size = 1) +  # Top 40m arc
    # Text Labels
      annotate("text", x = 8, y = 43, label = "Own 45", color = "white", size = 3) +  # Label bottom 45m line
    annotate("text", x = 8, y = 63, label = "Own 65", color = "white", size = 3) +  # Label bottom 65m line
    annotate("text", x = 7.5, y = pitch_length - 60, label = "Opp 65", color = "white", size = 3) +  # Label top 65m line
    annotate("text", x = 7.5, y = pitch_length - 41, label = "Opp 45", color = "white", size = 3) +  # Label top 45m line
    # Set theme
    theme_void() +
    theme(panel.background = element_rect(fill = "slateblue4", color = NA),
          plot.background = element_rect(fill = "slateblue4", color = NA),
          plot.margin = margin(0, 0, 0, 0)) +
    coord_fixed(ratio = 1)  # Ensure equal proportions
  
  print(pitch)
}

# Draw the GAA pitch without the title
draw_gaa_pitch_portrait()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

draw_takickout_drawing <- function() {
  # Define pitch dimensions
  pitch_length <- 105  # Pitch length ends at 100 meters
  pitch_width <- 90    # Maximum width in meters
  
  # Define six-yard box dimensions
  six_yard_box_length <- 14  # Length (x-direction)
  six_yard_box_width <- 4.5    # Width (y-direction)

  # Create base pitch
  pitch <- ggplot() +
    # Pitch outline
    geom_rect(aes(xmin = 0, xmax = pitch_width, ymin = 0, ymax = pitch_length), 
              fill = "slateblue4", color = "white", size = 1) +
    # Six-yard box
    geom_rect(aes(xmin = (pitch_width - six_yard_box_length) / 2, xmax = (pitch_width + six_yard_box_length) / 2, 
                  ymin = 0, ymax = six_yard_box_width), 
              fill = NA, color = "white", size = 1) +  # Bottom six-yard box
    # Goal areas
    geom_rect(aes(xmin = 35, xmax = 55, ymin = 0, ymax = 14), fill = NA, color = "white", size = 1) +
    # 13m, 21m, 45m, 65m, Opponent's 65m, and 100m lines
    geom_segment(aes(x = 0, xend = pitch_width, y = 14, yend = 14), color = "white", size = 1) +  # 13m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 21, yend = 21), color = "white", size = 1) +  # 21m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 45, yend = 45), color = "white", size = 1) +  # 45m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 65, yend = 65), color = "white", size = 1) +  # Own 65m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 72.5, yend = 72.5), 
                 color = "white", size = 1, linetype = "dashed") +  # Halfway line
    geom_segment(aes(x = 0, xend = pitch_width, y = 80, yend = 80), color = "white", size = 1) +  # Opponent's 65m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 100, yend = 100), color = "white", size = 1) +  # 100m line
    # Existing arcs
    annotate("path", x = pitch_width / 2 + 15 * cos(seq(pi, 0, length.out = 100)), 
             y = 21 + 13 * sin(seq(pi, 0, length.out = 100)), color = "white", size = 1) +  # Bottom arc
    annotate("path", x = pitch_width / 2 + 40 * cos(seq(pi, 0, length.out = 100)), 
             y = 21 + 19 * sin(seq(pi, 0, length.out = 100)), color = "white", size = 1) +  # 40m arc
    # Text Labels
    annotate("text", x = 6.5, y = 43, label = "Own 45", color = "white", size = 3.5) +  # Label 45m line
    annotate("text", x = 6.5, y = 63, label = "Own 65", color = "white", size = 3.5) +  # Label 65m line
    annotate("text", x = 6, y = 103, label = "Opp 45", color = "white", size = 3.5) +  # Label 100m line
    annotate("text", x = 6, y = 83, label = "Opp 65", color = "white", size = 3.5) +  # Label Opponent's 65m line
    # Set theme
    theme_void() +
    theme(panel.background = element_rect(fill = "slateblue4", color = NA),
          plot.background = element_rect(fill = "slateblue4", color = NA),
          plot.margin = margin(0, 0, 0, 0)) +
    coord_fixed(ratio = 1)  # Ensure equal proportions
  
  print(pitch)
}

# Draw the updated pitch
draw_takickout_drawing()

draw_tbkickout_drawing <- function() {
  # Define pitch dimensions
  pitch_length <- 105  # Pitch length ends at 100 meters
  pitch_width <- 90    # Maximum width in meters
  
  # Define six-yard box dimensions
  six_yard_box_length <- 14  # Length (x-direction)
  six_yard_box_width <- 4.5    # Width (y-direction)

  # Create base pitch
  pitch <- ggplot() +
    # Pitch outline
    geom_rect(aes(xmin = 0, xmax = pitch_width, ymin = 0, ymax = pitch_length), 
              fill = "slateblue4", color = "white", size = 1) +
        # Six-yard box
    geom_rect(aes(xmin = (pitch_width - six_yard_box_length) / 2, xmax = (pitch_width + six_yard_box_length) / 2, 
                  ymin = 0, ymax = six_yard_box_width), 
              fill = NA, color = "white", size = 1) +  # Bottom six-yard box
# Goal areas
    geom_rect(aes(xmin = 35, xmax = 55, ymin = 0, ymax = 14), fill = NA, color = "white", size = 1) +
    # 13m, 21m, 45m, 65m, Opponent's 65m, and 100m lines
    geom_segment(aes(x = 0, xend = pitch_width, y = 14, yend = 14), color = "white", size = 1) +  # 13m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 21, yend = 21), color = "white", size = 1) +  # 21m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 45, yend = 45), color = "white", size = 1) +  # 45m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 65, yend = 65), color = "white", size = 1) +  # Own 65m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 72.5, yend = 72.5), 
                 color = "white", size = 1, linetype = "dashed") +  # Halfway line
    geom_segment(aes(x = 0, xend = pitch_width, y = 80, yend = 80), color = "white", size = 1) +  # Opponent's 65m line
    geom_segment(aes(x = 0, xend = pitch_width, y = 100, yend = 100), color = "white", size = 1) +  # 100m line
    # Existing arcs
    annotate("path", x = pitch_width / 2 + 15 * cos(seq(pi, 0, length.out = 100)), 
             y = 21 + 13 * sin(seq(pi, 0, length.out = 100)), color = "white", size = 1) +  # Bottom arc
    annotate("path", x = pitch_width / 2 + 40 * cos(seq(pi, 0, length.out = 100)), 
             y = 21 + 19 * sin(seq(pi, 0, length.out = 100)), color = "white", size = 1) +  # 40m arc
    # Text Labels
    annotate("text", x = 6.5, y = 43, label = "Own 45", color = "white", size = 3.5) +  # Label 45m line
    annotate("text", x = 6.5, y = 63, label = "Own 65", color = "white", size = 3.5) +  # Label 65m line
    annotate("text", x = 6, y = 103, label = "Opp 45", color = "white", size = 3.5) +  # Label 100m line
    annotate("text", x = 6, y = 83, label = "Opp 65", color = "white", size = 3.5) +  # Label Opponent's 65m line
    # Set theme
    theme_void() +
    theme(panel.background = element_rect(fill = "slateblue4", color = NA),
          plot.background = element_rect(fill = "slateblue4", color = NA),
          plot.margin = margin(0, 0, 0, 0)) +
    coord_fixed(ratio = 1)  # Ensure equal proportions
  
  print(pitch)
}

# Draw the updated pitch
draw_tbkickout_drawing()

draw_ta_shots <- function() {
  # Define pitch dimensions for the top third
  pitch_length <- 145    # Maximum length of the full pitch
  pitch_width <- 90      # Maximum width of the pitch
  start_y <- 75          # Start of the top third
  end_y <- 145           # End of the top third
  
  # Define six-yard box dimensions
  six_yard_box_length <- 14  # Length (x-direction)
  six_yard_box_width <- 4.5    # Width (y-direction)

  # Create base pitch
  pitch <- ggplot() +
    # Pitch outline for the top third
    geom_rect(aes(xmin = 0, xmax = pitch_width, ymin = start_y, ymax = end_y), 
              fill = "slateblue4", color = "white", size = 1) +
            geom_rect(aes(xmin = (pitch_width - six_yard_box_length) / 2, xmax = (pitch_width + six_yard_box_length) / 2, 
                  ymin = pitch_length - six_yard_box_width, ymax = pitch_length), 
              fill = NA, color = "white", size = 1) +  # Top six-yard box
# Goal areas
    geom_rect(aes(xmin = 35, xmax = 55, ymin = pitch_length - 14, ymax = pitch_length), fill = NA, color = "white", size = 1) +
    # 13m, 21m, and 45m lines within the top third
    geom_segment(aes(x = 0, xend = pitch_width, y = end_y - 14, yend = end_y - 14), color = "white", size = 1) +  # 13m line
    geom_segment(aes(x = 0, xend = pitch_width, y = end_y - 21, yend = end_y - 21), color = "white", size = 1) +  # 21m line
    geom_segment(aes(x = 0, xend = pitch_width, y = end_y - 45, yend = end_y - 45), color = "white", size = 1) +  # 45m line
    geom_segment(aes(x = 0, xend = pitch_width, y = start_y, yend = start_y), color = "white", size = 1) +  # Bottom boundary of the top third
    # Arc at the top goal area
    annotate("path", x = pitch_width / 2 + 15 * cos(seq(pi, 2 * pi, length.out = 100)), 
             y = end_y - 21 + 13 * sin(seq(pi, 2 * pi, length.out = 100)), color = "white", size = 1) +  # Top 21m arc
    annotate("path", x = pitch_width / 2 + 40 * cos(seq(pi, 2 * pi, length.out = 100)), 
             y = end_y - 21 + 19 * sin(seq(pi, 2 * pi, length.out = 100)), color = "white", size = 1) +  # Top 40m arc
    # Text Labels
    annotate("text", x = 4.5, y = end_y - 43, label = "Opp 45", color = "white", size = 4) +  # Label 45m line
    annotate("text", x = 4.5, y = end_y - 68, label = "Opp 65", color = "white", size = 4) +  # Label 65m line
    # Set theme
    theme_void() +
    theme(panel.background = element_rect(fill = "slateblue4", color = NA),
          plot.background = element_rect(fill = "slateblue4", color = NA),
          plot.margin = margin(0, 0, 0, 0)) +
    coord_fixed(ratio = 1, xlim = c(0, pitch_width), ylim = c(start_y, end_y))  # Focus on the top third only
  
  print(pitch)
}

# Draw the pitch for the top third
draw_ta_shots()

draw_tb_shots <- function() {
  # Define pitch dimensions for the top third
  pitch_length <- 145    # Maximum length of the full pitch
  pitch_width <- 90      # Maximum width of the pitch
  start_y <- 75          # Start of the top third
  end_y <- 145           # End of the top third
  
  # Define six-yard box dimensions
  six_yard_box_length <- 14  # Length (x-direction)
  six_yard_box_width <- 4.5    # Width (y-direction)

  # Create base pitch
  pitch <- ggplot() +
    # Pitch outline for the top third
    geom_rect(aes(xmin = 0, xmax = pitch_width, ymin = start_y, ymax = end_y), 
              fill = "slateblue4", color = "white", size = 1) +
           geom_rect(aes(xmin = (pitch_width - six_yard_box_length) / 2, xmax = (pitch_width + six_yard_box_length) / 2, 
                  ymin = pitch_length - six_yard_box_width, ymax = pitch_length), 
              fill = NA, color = "white", size = 1) +  # Top six-yard box
# Goal areas
    geom_rect(aes(xmin = 35, xmax = 55, ymin = pitch_length - 14, ymax = pitch_length), fill = NA, color = "white", size = 1) +
    # 13m, 21m, and 45m lines within the top third
    geom_segment(aes(x = 0, xend = pitch_width, y = end_y - 14, yend = end_y - 14), color = "white", size = 1) +  # 13m line
    geom_segment(aes(x = 0, xend = pitch_width, y = end_y - 21, yend = end_y - 21), color = "white", size = 1) +  # 21m line
    geom_segment(aes(x = 0, xend = pitch_width, y = end_y - 45, yend = end_y - 45), color = "white", size = 1) +  # 45m line
    geom_segment(aes(x = 0, xend = pitch_width, y = start_y, yend = start_y), color = "white", size = 1) +  # Bottom boundary of the top third
    # Arc at the top goal area
    annotate("path", x = pitch_width / 2 + 15 * cos(seq(pi, 2 * pi, length.out = 100)), 
             y = end_y - 21 + 13 * sin(seq(pi, 2 * pi, length.out = 100)), color = "white", size = 1) +  # Top 21m arc
    annotate("path", x = pitch_width / 2 + 40 * cos(seq(pi, 2 * pi, length.out = 100)), 
             y = end_y - 21 + 19 * sin(seq(pi, 2 * pi, length.out = 100)), color = "white", size = 1) +  # Top 40m arc
    # Text Labels
    annotate("text", x = 4.5, y = end_y - 43, label = "Opp 45", color = "white", size = 4) +  # Label 45m line
    annotate("text", x = 4.5, y = end_y - 68, label = "Opp 65", color = "white", size = 4) +  # Label 65m line
    # Set theme
    theme_void() +
    theme(panel.background = element_rect(fill = "slateblue4", color = NA),
          plot.background = element_rect(fill = "slateblue4", color = NA),
          plot.margin = margin(0, 0, 0, 0)) +
    coord_fixed(ratio = 1, xlim = c(0, pitch_width), ylim = c(start_y, end_y))  # Focus on the top third only
  
  print(pitch)
}

# Draw the pitch for the top third
draw_tb_shots()

# Assuming 'Test_again' dataset is already loaded in your environment

# Count occurrences of "TA Kickout" and "TB Kickout"
ta_kickout_count <- sum(Test_again$Kickouts == "TA Kickout", na.rm = TRUE)
tb_kickout_count <- sum(Test_again$Kickouts == "TB Kickout", na.rm = TRUE)

# Add them together to get the total
total_kickouts <- ta_kickout_count + tb_kickout_count

# Print the results
cat("KER Kickouts:", ta_kickout_count, "\n")
KER Kickouts: 1 
cat("OPP Kickouts:", tb_kickout_count, "\n")
OPP Kickouts: 1 
cat("Total Kickouts:", total_kickouts, "\n")
Total Kickouts: 2 
# Assuming 'Test_again' dataset is already loaded

# Count the occurrences where "TA Short" appears in the TA Kickout Location column
# and "TA Won" appears in the Kickout Outcome column
ta_short_won_count <- sum(Test_again$`TA Kickout Location` == "TA SKO" & Test_again$`Kickout Outcome` == "TA Won", na.rm = TRUE)

# Count the occurrences where "TA Short" appears in the TA Kickout Location column
# and "TA Lost" appears in the Kickout Outcome column
ta_short_lost_count <- sum(Test_again$`TA Kickout Location` == "TA SKO" & Test_again$`Kickout Outcome` == "TA Lost", na.rm = TRUE)

# Count the occurrences where "TA Long" appears in the TA Kickout Location column
# and "TA Won" appears in the Kickout Outcome column
ta_long_won_count <- sum(Test_again$`TA Kickout Location` == "TA LKO" & Test_again$`Kickout Outcome` == "TA Won", na.rm = TRUE)

# Count the occurrences where "TA Long" appears in the TA Kickout Location column
# and "TA Lost" appears in the Kickout Outcome column
ta_long_lost_count <- sum(Test_again$`TA Kickout Location` == "TA LKO" & Test_again$`Kickout Outcome` == "TA Lost", na.rm = TRUE)

# Total Short count (Won + Lost)
total_short_count <- ta_short_won_count + ta_short_lost_count

# Total Long count (Won + Lost)
total_long_count <- ta_long_won_count + ta_long_lost_count

# Calculate total kickouts won
total_kickouts_won <- ta_short_won_count + ta_long_won_count

# Calculate total kickouts taken
total_kickouts_taken <- total_short_count + total_long_count

# Calculate win percentage
win_percentage <- (total_kickouts_won / total_kickouts_taken) * 100

# Create a data frame for the table without the Total column
kickouts_table <- data.frame(
  KER = c("Won", "Lost", "Total"),  # Rename the Outcome column to KER
  Short = c(ta_short_won_count, ta_short_lost_count, total_short_count),  # Renamed Short to Shrt
  Long = c(ta_long_won_count, ta_long_lost_count, total_long_count)  # Renamed Long Kickouts column
)

# Load knitr and kableExtra for creating the table
library(knitr)
library(kableExtra)

# Render the table with color and titles, centered content
kable(kickouts_table, 
      col.names = c("KER", "Short", "Long"),
      format = "html", 
      align = "c") %>%  # Center-align all columns
  kable_styling(bootstrap_options = c("striped", "hover"), full_width = F) %>%
  column_spec(1, color = "black", background = "white") %>%
  column_spec(2, color = "black", background = "white") %>%
  column_spec(3, color = "black", background = "white") %>%
  row_spec(0, background = "#28a745", color = "white", bold = TRUE)  # Highlight top row (column headers) in green
KER Short Long
Won 1 0
Lost 0 0
Total 1 0
# Print the win percentage and total information
cat("KER KO Won:", round(win_percentage, 2), "%", 
    "(", total_kickouts_won, "/", total_kickouts_taken, ")\n")
KER KO Won: 100 % ( 1 / 1 )
# Assuming 'Test_again' dataset is already loaded

# Count the occurrences where "TB Short" appears in the TB Kickout Location column
# and "TA Won" appears in the Kickout Outcome column
tb_short_won_count <- sum(Test_again$`TB Kickout Location` == "TB SKO" & Test_again$`Kickout Outcome` == "TA Won", na.rm = TRUE)

# Count the occurrences where "TB Short" appears in the TB Kickout Location column
# and "TB Lost" appears in the Kickout Outcome column
tb_short_lost_count <- sum(Test_again$`TB Kickout Location` == "TB SKO" & Test_again$`Kickout Outcome` == "TA Lost", na.rm = TRUE)

# Count the occurrences where "TB Long" appears in the TB Kickout Location column
# and "TB Won" appears in the Kickout Outcome column
tb_long_won_count <- sum(Test_again$`TB Kickout Location` == "TB LKO" & Test_again$`Kickout Outcome` == "TA Won", na.rm = TRUE)

# Count the occurrences where "TB Long" appears in the TB Kickout Location column
# and "TB Lost" appears in the Kickout Outcome column
tb_long_lost_count <- sum(Test_again$`TB Kickout Location` == "TB LKO" & Test_again$`Kickout Outcome` == "TA Lost", na.rm = TRUE)

# Total Short count (Won + Lost)
total_tb_short_count <- tb_short_won_count + tb_short_lost_count

# Total Long count (Won + Lost)
total_tb_long_count <- tb_long_won_count + tb_long_lost_count

# Total kickouts won
total_tb_kickouts_won <- tb_short_won_count + tb_long_won_count

# Total kickouts taken
total_tb_kickouts_taken <- total_tb_short_count + total_tb_long_count

# Calculate win percentage
opp_win_percentage <- (total_tb_kickouts_won / total_tb_kickouts_taken) * 100

# Load knitr for creating the table
library(knitr)

# Create a data frame for the table with separate columns for Short and Long Kickouts
kickouts_table_opposite <- data.frame(
  OPP = c("Won", "Lost", "Total"),  # Rename the Outcome column to OPP
  Short = c(tb_short_won_count, tb_short_lost_count, total_tb_short_count),  # Renamed Short to Short
  Long = c(tb_long_won_count, tb_long_lost_count, total_tb_long_count)  # Renamed Long Kickouts column
)

# Render the table with color and titles, centered content
kable(kickouts_table_opposite, 
      col.names = c("OPP", "Short", "Long"),
      format = "markdown", 
      align = "c") %>%  # Center-align all columns
  kable_styling(bootstrap_options = c("striped", "hover"), full_width = F) %>%
  column_spec(1, color = "black", background = "white") %>%
  column_spec(2, color = "black", background = "white") %>%
  column_spec(3, color = "black", background = "white") %>%
  row_spec(0, background = "black", color = "white", bold = TRUE)  # Highlight top row (column headers) in black
OPP Short Long
Won 0 0
Lost 1 0
Total 1 0
# Print the win percentage and total information
cat("OPP KO Won:", round(opp_win_percentage, 2), "%", 
    "(", total_tb_kickouts_won, "/", total_tb_kickouts_taken, ")\n")
OPP KO Won: 0 % ( 0 / 1 )
# Initialize counters
ta_clean_won <- 0
ta_clean_lost <- 0
ta_break_won <- 0
ta_break_lost <- 0

# Loop through the dataset to calculate counts
for (i in 2:nrow(Test_again)) {  # Start from the second row
  if (!is.na(Test_again$Kickouts[i - 1]) && Test_again$Kickouts[i - 1] == "TA Kickout") {  
    if (Test_again$`TA Won Kickout Breakdown`[i] == "TA Clean") {
      ta_clean_won <- ta_clean_won + 1
    } else if (Test_again$`TA Won Kickout Breakdown`[i] == "TB Clean") {
      ta_clean_lost <- ta_clean_lost + 1
    } else if (Test_again$`TA Won Kickout Breakdown`[i] == "TA Break") {
      ta_break_won <- ta_break_won + 1
    } else if (Test_again$`TA Won Kickout Breakdown`[i] == "TB Break") {
      ta_break_lost <- ta_break_lost + 1
    }
  }
}

# Calculate totals
total_clean <- ta_clean_won + ta_clean_lost
total_break <- ta_break_won + ta_break_lost

# Create a data frame with KER, Clean, and Break as columns
table_data <- data.frame(
  KER = c("Won", "Lost", "Total"),
  Clean = c(ta_clean_won, ta_clean_lost, total_clean),
  Break = c(ta_break_won, ta_break_lost, total_break)
)

# Load knitr and kableExtra for table styling
library(knitr)
library(kableExtra)

# Render the table with the correct layout
kable(table_data, 
      col.names = c("KER", "Clean", "Break"),  # Set the column headers
      format = "html", 
      align = "c") %>%
  kable_styling(bootstrap_options = c("striped", "hover"), full_width = F) %>%
  row_spec(0, background = "#28a745", color = "white", bold = TRUE) %>%  # Green header row
  row_spec(2, background = "#f8f8f5")  # Light shading for "Lost" row
KER Clean Break
Won 1 0
Lost 0 0
Total 1 0
# Assuming 'Test_again' dataset is already loaded

# Count the occurrences of "Turnover Won"
turnover_won_count <- sum(Test_again$Turnovers == "Turnover Won", na.rm = TRUE)

# Count the occurrences of "Turnover Lost"
turnover_lost_count <- sum(Test_again$Turnovers == "Turnover Lost", na.rm = TRUE)

# Calculate the total turnovers
total_turnovers <- turnover_won_count + turnover_lost_count

# Print the results
cat("Turnover Won:", turnover_won_count, "\n")
Turnover Won: 3 
cat("Turnover Lost:", turnover_lost_count, "\n")
Turnover Lost: 4 
cat("Total Turnovers:", total_turnovers, "\n")
Total Turnovers: 7 
# Assuming 'Test_again' dataset is already loaded

# Count the occurrences for each category in the DEF column
def_won <- sum(Test_again$`Turnover Location` == "TA Defensive Third TO", na.rm = TRUE)
def_lost <- sum(Test_again$`Turnover Location` == "TB Defensive Third TO", na.rm = TRUE)
def_total <- def_won + def_lost

# Count the occurrences for each category in the MID column
mid_won <- sum(Test_again$`Turnover Location` == "TA Middle Third TO", na.rm = TRUE)
mid_lost <- sum(Test_again$`Turnover Location` == "TB Middle Third TO", na.rm = TRUE)
mid_total <- mid_won + mid_lost

# Count the occurrences for each category in the ATT column
att_won <- sum(Test_again$`Turnover Location` == "TA  Attacking Third TO", na.rm = TRUE)
att_lost <- sum(Test_again$`Turnover Location` == "TB  Attacking Third TO", na.rm = TRUE)
att_total <- att_won + att_lost

# Create a data frame for the table
turnover_table <- data.frame(
  Area = c("Won", "Lost", "Total"),
  DEF = c(def_won, def_lost, def_total),
  MID = c(mid_won, mid_lost, mid_total),
  ATT = c(att_won, att_lost, att_total)
)

# Load knitr and kableExtra for creating the table
library(knitr)
library(kableExtra)

# Render the table
kable(turnover_table, 
      col.names = c("Area", "DEF", "MID", "ATT"), 
      format = "html", 
      align = "c") %>%
  kable_styling(bootstrap_options = c("striped", "hover"), full_width = F) %>%
  column_spec(1, color = "black", background = "white") %>%
  column_spec(2:4, color = "black", background = "white") %>%
  row_spec(0, background = "#000000", color = "white", bold = TRUE)  # Highlight the top row in black
Area DEF MID ATT
Won 1 1 1
Lost 2 1 1
Total 3 2 2
library(ggplot2)

# Create a data frame for turnover_long with calculated counts
turnover_long <- data.frame(
  Area = rep(c("Lost", "Won"), each = 3),  # Repeat "Lost" and "Won" for each AreaType
  AreaType = c("DEF", "MID", "ATT", "DEF", "MID", "ATT"),  # Area types (DEF, MID, ATT)
  Count = c(def_lost, mid_lost, att_lost, def_won, mid_won, att_won)  # The counts from above
)

# Reorder the levels of 'Area' and 'AreaType' to control the ordering in the plot
turnover_long$Area <- factor(turnover_long$Area, levels = c("Won", "Lost"))
turnover_long$AreaType <- factor(turnover_long$AreaType, levels = c("ATT", "MID", "DEF"))  # Swap DEF and ATT

# Create the horizontal bar chart
ggplot(turnover_long, aes(x = Area, y = Count, fill = AreaType)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.45), width = 0.45) +  # Side-by-side bars
  geom_text(aes(label = Count), 
            position = position_dodge(width = 0.45),  # Align text with the dodged bars
            color = "white", 
            size = 5, 
            fontface = "bold",
            hjust = 2) +  # Bold text labels
  scale_fill_manual(values = c("DEF" = "red", "MID" = "darkorange", "ATT" = "darkgreen")) +  # Match colors
  coord_flip() +  # Flip coordinates for horizontal bars
  theme_void() +  # Remove all background graphics
  theme(
    legend.position = "none",  # Remove legend
    axis.text.y = element_text(size = 10, color = "black", face = "bold"),  # Bold Won/Lost labels
    axis.text.x = element_blank(),  # Remove X-axis text
    axis.ticks = element_blank(),  # Remove axis ticks
    panel.grid = element_blank()  # Remove grid lines
  )