Flg. er et hurtigt simpelt script til at visualisere xg (expected goals) fra en enkelt kamp via R. Scriptet kan inspirere til at inddrage flere kampe, merge tabeller eller hvad ved jeg.
library(jsonlite)
library(ggplot2)
# URL til JSON data
url <- "https://api.superliga.dk/opta-stats/event/4192411/detail-expected-goals?appName=superligadk&access_token=5b6ab6f5eb84c60031bbbd24&env=production&locale=da" # Erstat denne URL med den faktiske URL
# Hent JSON data direkte fra URL og læs ind i et R objekt
json_data <- fromJSON(url)
# Brug json_data som nødvendigt i dit script
# Extract expected goals data for both teams
home_data <- json_data$expectedGoalsData$home
away_data <- json_data$expectedGoalsData$away
# Convert to data frames
home_df <- as.data.frame(home_data)
away_df <- as.data.frame(away_data)
# Combine home and away data for plotting
combined_df <- rbind(home_df, away_df)
combined_df$x <- as.numeric(combined_df$x)
combined_df$y <- as.numeric(combined_df$y)
# Plotting
ggplot(combined_df, aes(x = x, y = y, color = expectedGoalsValue, size = expectedGoalsValue)) +
geom_point() +
scale_color_gradient(low = "blue", high = "red") +
theme_minimal() +
labs(title = "Expected Goals Plot", x = "Distance from Middle to Goal Line", y = "Side Line to Side Line") +
coord_fixed(ratio = 1) + # Keep the aspect ratio of the pitch consistent
geom_rect(aes(xmin=0, xmax=100, ymin=0, ymax=100), color="black", fill=NA) # Outline the pitch