abhiamishra / ggshakeR

An analysis and visualization R package that works with publicly available soccer data
https://abhiamishra.github.io/ggshakeR/
MIT License
113 stars 14 forks source link

[Bug]: Timeline flow chart #115

Closed MaundoJako closed 2 years ago

MaundoJako commented 2 years ago

What happened?

ggshakeR version: 0.2.0.9000 R version: RStudio 2022.07.1+554 "Spotted Wakerobin" Release (7872775ebddc40635780ca1ed238934c3345c5de, 2022-07-22) for macOS Mozilla/5.0 (Macintosh; Intel Mac OS X 12_0_1) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.10 Chrome/69.0.3497.128 Safari/537.36

Code matches the example here: https://github.com/ggshakeR/ggshakeR.github.io/blob/main/gallery/plots/Timeline%20Flow.R

However, the resulting plot is wrong as seen by the screenshot.

Data: 0-1 win.xlsx

image

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.1.2
#> Warning: package 'ggplot2' was built under R version 4.1.2
#> Warning: package 'tibble' was built under R version 4.1.2
#> Warning: package 'tidyr' was built under R version 4.1.2
#> Warning: package 'readr' was built under R version 4.1.2
#> Warning: package 'dplyr' was built under R version 4.1.2
library(ggtext)
library(ggbraid)
#> Warning: package 'ggbraid' was built under R version 4.1.2
library(ggshakeR)

df <- readxl::read_xlsx("Documents/Football/Coding/Data/Stoke match data/22.23/Blackburn Rovers (a) 27.08.22/0-1 win.xlsx")
#> Error: `path` does not exist: 'Documents/Football/Coding/Data/Stoke match data/22.23/Blackburn Rovers (a) 27.08.22/0-1 win.xlsx'

df <- df %>%
  select(x, y, endX, endY, teamId, minute, type) %>%
  rename("finalX" = "endX", "finalY" = "endY") %>%
  mutate(teamId = case_when(teamId == 96 ~ "Stoke City", teamId == 158 ~ "Blackburn Rovers")) %>%
  filter(type == "Pass")
#> Error in UseMethod("select"): no applicable method for 'select' applied to an object of class "function"

data <- df %>%
  ggshakeR::calculate_threat() %>%
  mutate(xT = xTEnd - xTStart) %>%
  mutate(xT = tidyr::replace_na(xT, 0))
#> Error in ggshakeR::calculate_threat(.): Dataframe has insufficient number of rows and/or you don't have the right amount of columns: `x`, `y`, `finalX`, `finalY`
view(data)

data1 <- data %>%
  filter(teamId == "Stoke City") %>%
  mutate(xT = ifelse(xT > 0, xT, -xT))
#> Error in UseMethod("filter"): no applicable method for 'filter' applied to an object of class "function"
data2 <- data %>%
  filter(teamId == "Blackburn Rovers") %>%
  mutate(xT = ifelse(xT > 0, xT, -xT))
#> Error in UseMethod("filter"): no applicable method for 'filter' applied to an object of class "function"

data <- rbind(data1,data2) %>%
  group_by(minute) %>%
  summarise(xT = sum(xT))
#> Error in rbind(data1, data2): object 'data1' not found

spline_int <- as.data.frame(spline(data$minute, data$xT)) %>%
  mutate(teamId = ifelse(y > 0, "Stoke City", "Blackburn Rovers"))
#> Error in data$xT: object of type 'closure' is not subsettable

theme_custom <- function() {
  theme_minimal() +
    theme(plot.background = element_rect(colour = "#14171A", fill = "#14171A"),
          panel.background = element_rect(colour = "#14171A", fill = "#14171A")) +
    theme(plot.title = element_text(colour = "white", size = 16, hjust = 0.5, face = "bold"),
          plot.subtitle = element_markdown(colour = "white", size = 12, hjust = 0.5),
          plot.caption = element_text(colour = "white", size = 10, hjust = 1),
          axis.title.x = element_text(colour = "white", face = "bold", size = 10),
          axis.title.y = element_text(colour = "white", face = "bold", size = 10),
          axis.text.x = element_text(colour = "white", size = 9),
          axis.text.y = element_text(colour = "white", size = 9)) +
    theme(panel.grid.major = element_line(colour = "#525252", size = 0.4, linetype = "dashed"),
          panel.grid.minor = element_line(colour = "#525252", size = 0.4, linetype = "dashed")) +
    theme(panel.grid.major.x = element_line(colour = "#525252", size = 0.4, linetype = "dashed"),
          panel.background = element_blank()) +
    theme(legend.position = "bottom",
          legend.text = element_text(colour = "white"),
          legend.title = element_text(colour = "white"))
}

ggplot(spline_int) +
  geom_braid(aes(x = x, ymin = 0, ymax = y, fill = teamId)) +
  scale_fill_manual(values = c("deepskyblue3", "firebrick3")) +
  geom_line(aes(x = x, y = y), linetype = "longdash", colour = "white") +
  geom_hline(yintercept = 0, colour = "white", linetype = "solid", size = 0.5) +
  ylim(-0.5, 0.5) +
  theme_custom() +
  labs(x = "Minute", y = "xT Difference", 
       title = "Blackburn Rovers vs. Stoke City xT Flow", 
       subtitle = "2022/23 Championship | GW 6",
       fill = "Team") +
  scale_x_continuous(breaks = seq(from = 0, to = 90, by = 10))
#> Error in ggplot(spline_int): object 'spline_int' not found

ggsave("xTFlow.png", bg = "#14171A", width = 2600, height = 1400, units = "px")
harshkrishna17 commented 2 years ago

closed as done