Skip to content

Commit

Permalink
Optionally enable customisation of the penalty arc
Browse files Browse the repository at this point in the history
The true radius should be 10 yards, not 12 (penalty distance).
However this can look weird on some dimensions. Enabling
customisation allows users to circumvent this.

Dimensions which can support the true radius have been amended.

See #35

Closes #35
  • Loading branch information
Torvaney committed Apr 4, 2024
1 parent 282c1b1 commit 4b8ad2e
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 11 deletions.
6 changes: 3 additions & 3 deletions R/annotate_pitch.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ annotate_base_pitch <- function(colour, fill, spec, linewidth, alpha, linetype)
annotate_circle(
x = midpoint$x,
y = midpoint$y,
r = spec$penalty_spot_distance,
r = spec$penalty_arc_radius %||% spec$penalty_spot_distance,
colour = colour,
linewidth = linewidth,
linetype = linetype,
Expand Down Expand Up @@ -164,7 +164,7 @@ annotate_penalty_box <- function(colour, dimensions, spec, linewidth, alpha, lin
xintercept = spec$origin_x + spec$length - spec$penalty_box_length,
x0 = spec$origin_x + spec$length - spec$penalty_spot_distance,
y0 = midpoint$y,
r = spec$penalty_spot_distance,
r = spec$penalty_arc_radius %||% spec$penalty_spot_distance,
direction = "left",
colour = colour,
linewidth = linewidth,
Expand Down Expand Up @@ -218,7 +218,7 @@ annotate_penalty_box <- function(colour, dimensions, spec, linewidth, alpha, lin
xintercept = spec$origin_x + spec$penalty_box_length,
x0 = spec$origin_x + spec$penalty_spot_distance,
y0 = midpoint$y,
r = spec$penalty_spot_distance,
r = spec$penalty_arc_radius %||% spec$penalty_spot_distance,
direction = "right",
colour = colour,
linewidth = linewidth,
Expand Down
13 changes: 9 additions & 4 deletions R/dimensions.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#' \item{"goal_width"}{The distance from one goal post to the other}
#' \item{"origin_x"}{The minimum x coordinate of the pitch}
#' \item{"origin_y"}{The minimum y coordinate of the pitch}
#' \item{"penalty_arc_radius"}{The radius of the arc above the penalty box (Optional). Defaults to `penalty_spot_distance`}
#' }
#'
#' The following pitch dimensions are provided
Expand Down Expand Up @@ -75,7 +76,8 @@ pitch_statsbomb <- list(
penalty_spot_distance = 12,
goal_width = 8,
origin_x = 0,
origin_y = 0
origin_y = 0,
penalty_arc_radius = 10
)

#' @rdname pitch_opta
Expand All @@ -98,6 +100,7 @@ pitch_wyscout <- list(
#' @export
# As per UEFA Category 4 regulations
# Source: https://en.wikipedia.org/wiki/UEFA_stadium_categories
# Source: https://en.wikipedia.org/wiki/Penalty_area
pitch_international <- list(
length = 105,
width = 68,
Expand All @@ -108,7 +111,8 @@ pitch_international <- list(
penalty_spot_distance = 11,
goal_width = 7.32,
origin_x = 0,
origin_y = 0
origin_y = 0,
penalty_arc_radius = 9.15
)


Expand Down Expand Up @@ -152,7 +156,7 @@ make_pitch_tracab <- function(length=105, width=68){
#' @export
pitch_tracab <- make_pitch_tracab()


#' @rdname pitch_opta
#' @export
# Source: https://glossary.impect.com/api-design?tab=2
Expand All @@ -166,5 +170,6 @@ pitch_impect <- list(
penalty_spot_distance = 11,
goal_width = 7.32,
origin_x = -52.5,
origin_y = -34
origin_y = -34,
penalty_arc_radius = 10
)
Binary file modified man/figures/README-example_blank-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-example_custom-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-example_further_customisation-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-example_goals_custom-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-example_goals_line-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-example_goals_strip-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-example_passes-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-example_passes_sb-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-example_shots-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 5 additions & 4 deletions man/pitch_opta.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions tests/testthat/test-dimensions-plot-without-error.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,62 @@ test_that("Plotting valid pitch dimensions with a negative origin plots without
})


test_that("Plotting dimensions without a `penalty_arc_radius` doesn't error", {
dim <- list(
length = 100,
width = 100,
penalty_box_length = 17,
penalty_box_width = 57.8,
six_yard_box_length = 5.8,
six_yard_box_width = 26.4,
penalty_spot_distance = 11.5,
goal_width = 11.6,
origin_x = 0,
origin_y = 0
)

expect_no_error({
ggplot2::ggplot() +
annotate_pitch(dimensions = dim)
})

# Also check for warnings
expect_no_warning({
ggplot2::ggplot() +
annotate_pitch(dimensions = dim)
})
})


test_that("Plotting dimensions with a `penalty_arc_radius` doesn't error", {
dim <- list(
length = 100,
width = 100,
penalty_box_length = 17,
penalty_box_width = 57.8,
six_yard_box_length = 5.8,
six_yard_box_width = 26.4,
penalty_spot_distance = 11.5,
goal_width = 11.6,
origin_x = 0,
origin_y = 0,
penalty_arc_radius = 10
)

expect_no_error({
ggplot2::ggplot() +
annotate_pitch(dimensions = dim)
})

# Also check for warnings
# arcs
expect_no_warning({
ggplot2::ggplot() +
annotate_pitch(dimensions = dim)
})
})





Expand Down

0 comments on commit 4b8ad2e

Please sign in to comment.