Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable additional aesthetics in pitch annotation (linewidth, alpha, linetype) #27

Merged
merged 18 commits into from
Dec 13, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make goals take aesthetic arguments
Torvaney committed Nov 30, 2022
commit efd61df8ce61c056018fc6e7249454ea5c383061
21 changes: 16 additions & 5 deletions R/annotate_pitch.R
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ annotate_pitch <- function(colour = "dimgray",
annotate_base_pitch(colour, fill, dimensions, size, alpha, linetype),
annotate_penalty_box(colour, fill, dimensions, size, alpha, linetype),
annotate_six_yard_box(colour, fill, dimensions, size, alpha, linetype),
goals_f(colour = colour, fill = fill, dimensions = dimensions)
goals_f(colour = colour, fill = fill, dimensions = dimensions, size = size, alpha = alpha, linetype = linetype)
), recursive = FALSE)

if (!limits) {
@@ -309,7 +309,7 @@ annotate_circle <- function(x, y, r, ...) {
#' geom_point()
#'
#' @export
goals_box <- function(colour, fill, dimensions, offset = 2, ...) {
goals_box <- function(colour, fill, dimensions, size = 1, alpha = 1, linetype = "solid", offset = 2, ...) {
midpoint <- pitch_center(dimensions)

list(
@@ -321,6 +321,9 @@ goals_box <- function(colour, fill, dimensions, offset = 2, ...) {
ymax = midpoint$y + dimensions$goal_width/2,
colour = colour,
fill = fill,
size = size,
alpha = alpha,
linetype = linetype,
...
),
ggplot2::annotate(
@@ -331,6 +334,9 @@ goals_box <- function(colour, fill, dimensions, offset = 2, ...) {
ymax = midpoint$y + dimensions$goal_width/2,
colour = colour,
fill = fill,
size = size,
alpha = alpha,
linetype = linetype,
...
)
)
@@ -339,7 +345,7 @@ goals_box <- function(colour, fill, dimensions, offset = 2, ...) {
#' @rdname goals_box
#' @importFrom rlang %||%
#' @export
goals_strip <- function(colour, fill, dimensions, offset = 1, size = 1, lineend = "round", ...) {
goals_strip <- function(colour, fill, dimensions, size = 1, alpha = 1, linetype = "solid", offset = 1, lineend = "round", ...) {
midpoint <- pitch_center(dimensions)

list(
@@ -351,6 +357,7 @@ goals_strip <- function(colour, fill, dimensions, offset = 1, size = 1, lineend
yend = midpoint$y + dimensions$goal_width/2,
colour = colour,
size = size,
linetype = linetype,
lineend = lineend,
...
),
@@ -362,6 +369,7 @@ goals_strip <- function(colour, fill, dimensions, offset = 1, size = 1, lineend
yend = midpoint$y + dimensions$goal_width/2,
colour = colour,
size = size,
linetype = linetype,
lineend = lineend,
...
)
@@ -370,8 +378,11 @@ goals_strip <- function(colour, fill, dimensions, offset = 1, size = 1, lineend

#' @rdname goals_box
#' @export
goals_line <- function(colour, fill, dimensions, ...) {
goals_strip(colour, fill, dimensions, offset = 0, size = 1.5, ...)
goals_line <- function(colour, fill, dimensions, ..., size = 1, relative_size = 3) {
# We want the goals line to be responsive to the size of the rest of the pitch markings
# To do this, we multiply the provided size for the rest of the pitch markings by
# a scaling factor, `relative_size`
goals_strip(colour, fill, dimensions, size = size*relative_size, ..., offset = 0)
}

# Helper functions
9 changes: 8 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
@@ -192,10 +192,17 @@ you can modify the supplied functions, or create your own goal markings function
Additionally, the `goals` argument supports using one-sided formulas as lambda functions
(see [`rlang::as_function`](https://rlang.r-lib.org/reference/as_function.html)).

Custom goals functions must accept the arguments used by `annotate_pitch`: `colour`, `fill`,
`dimensions`, `size`, `alpha`, and `linetype`. Additional arguments can also be added.

```{r example_goals_custom}
goals_custom <- function(colour, fill, dimensions, ...) {
goals_strip(colour, fill, dimensions, lineend = "square", size = 3.5)
}

ggplot() +
annotate_pitch(
goals = ~ goals_strip(..., lineend = "square", size = 3.5),
goals = goals_custom,
fill = "lightgray"
) +
theme_pitch()
29 changes: 24 additions & 5 deletions man/goals_box.Rd

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