Skip to content

Commit

Permalink
Fixing functions and adding trademark caption (#113)
Browse files Browse the repository at this point in the history
- Closes #111 by fixing plot_pass, plot_heatmap, and plot_passflow orientation + plotting problems
- Works on #110 by adding "Created using ggshakeR" to the above plots.
  • Loading branch information
abhiamishra authored Aug 21, 2022
1 parent 250c72b commit 3293fce
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 34 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- Fixed `plot_heatmap` verification assessment [issue #101](https://github.com/abhiamishra/ggshakeR/issues/101)
- Fixed pkgdown build issues [issue #104](https://github.com/abhiamishra/ggshakeR/issues/104)
- Fixed `plot_passnet` direction orientation [issue #108](https://github.com/abhiamishra/ggshakeR/issues/108)
- Fixed `plot_heatmap` and `plot_passflow`direction orientation + `plot_pass`opta functionality was fixed.[issue #111](https://github.com/abhiamishra/ggshakeR/issues/111)
- Added "Created using ggshakeR" to `plot_heatmap`, `plot_passflow`, and `plot_pass`[issue 110](https://github.com/abhiamishra/ggshakeR/issues/110)

# ggshakeR 0.2.0

Expand Down
9 changes: 5 additions & 4 deletions R/plot_heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ plot_heatmap <- function(data, type = "", data_type = "statsbomb", binwidth = 20

if (type == "" || type == "density") {
plot <- plot +
stat_density_2d(aes(x = x, y = 80 - y, fill = ..level..), geom = "polygon")
stat_density_2d(aes(x = x, y = y, fill = ..level..), geom = "polygon")
} else if (type == "hex") {
plot <- plot +
geom_hex(aes(x = x, y = 80 - y))
geom_hex(aes(x = x, y = y))
} else if (type == "binwidth") {
plot <- plot +
geom_bin2d(aes(x = x, y = 80 - y),
geom_bin2d(aes(x = x, y = y),
binwidth = c(binwidth, binwidth),
alpha = 0.9)
} else if (type == "jdp") {
Expand Down Expand Up @@ -96,7 +96,8 @@ plot_heatmap <- function(data, type = "", data_type = "statsbomb", binwidth = 20
plot <- plot +
scale_fill_continuous(type = "viridis") +
labs(
fill = "Density"
fill = "Density",
caption = "Created using ggshakeR"
)

return(plot)
Expand Down
48 changes: 27 additions & 21 deletions R/plot_pass.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#'
#' @param data The data frame that stores your passing data. Opta data frame must contain at least the following columns: `x`, `y`, `finalX`, `finalY`
#' @param data_type Type of data that is being put in: opta or statsbomb. Default set to "statsbomb"
#' @param type indicates the type of plot to pass. "sep" separates successful and unsuccessful passes. "all" plots all passes on one pitch. Default = "sep"
#' @param progressive_pass indicates whether to map out progressive passes
#' @param cross indicates whether to map out crosses
#' @param shot indicates whether to map out shot assists
#' @param switch indicates whether to map out switches of play
#' @param outcome indicates whether you want successful ("suc"), unsuccessful ("unsuc"), or all ("all")
#' @param type indicates the type of plot to pass. "sep" separates successful and unsuccessful passes. "all" plots all passes on one pitch. Default = "sep". Only available for StatsBomb data
#' @param progressive_pass indicates whether to map out progressive passes. Only available for StatsBomb data
#' @param cross indicates whether to map out crosses. Only available for StatsBomb data
#' @param shot indicates whether to map out shot assists. Only available for StatsBomb data
#' @param switch indicates whether to map out switches of play. Only available for StatsBomb data
#' @param outcome indicates whether you want successful ("suc"), unsuccessful ("unsuc"), or all ("all"). Only available for StatsBomb data
#' @param theme indicates what theme the map must be shown in: dark (default), white, rose, almond
#' @return returns a ggplot2 object
#'
Expand Down Expand Up @@ -45,18 +45,20 @@ plot_pass <- function(data, data_type = "statsbomb", type = "sep",
color_b <- "#696969"
}


if (data_type == "opta") { ## OPTA ----

if (nrow(data) > 0 &&
sum(x = c("x", "y", "finalX", "finalY") %in% names(data)) == 4) {
} else {
print(c("x", "y", "finalX", "finalY"))
if (nrow(data) <= 0 ||
sum(x = c("x", "y", "finalX", "finalY") %in% names(data)) != 4) {
stop("The dataset has insufficient columns and/or insufficient data.")
}

to_sb <- rescale_coordinates(from = pitch_opta, to = pitch_statsbomb)

data$x <- to_sb$x(data$x)
data$y <- to_sb$y(data$y)
data$finalX <- to_sb$x(data$finalX)
data$finalY <- to_sb$y(data$finalY)

if (progressive_pass == TRUE) {
data <- data %>%
Expand All @@ -76,18 +78,20 @@ plot_pass <- function(data, data_type = "statsbomb", type = "sep",
theme(panel.background = element_rect(fill = fill_b))

plot <- plot +
geom_segment(aes(x = x, y = 80 - y,
xend = finalX, yend = 80 - finalY, color = "red"),
geom_segment(aes(x = x, y = y,
xend = finalX, yend = finalY, color = "red"),
lineend = "round", size = 1.5, arrow = arrow(length = unit(0.10, "inches")),
stat = "identity", position = "identity") +
labs(color = "Outcome of Pass")
labs(color = "Outcome of Pass",
caption = "Created using ggshakeR") +
theme(plot.caption = element_text(color = "black"))

return(plot)

} else if (data_type == "statsbomb") { ## STATSBOMB ----

if (nrow(data) > 0) {
} else {
if (nrow(data) <= 0 ||
sum(x = c("x", "y", "finalX", "finalY") %in% names(data)) != 4) {
stop("The dataset has insufficient columns and/or insufficient data.")
}

Expand Down Expand Up @@ -154,22 +158,24 @@ plot_pass <- function(data, data_type = "statsbomb", type = "sep",
if (nrow(data) > 0) {
if (type == "sep") {
plot <- plot +
geom_segment(aes(x = x, y = 80 - y,
xend = finalX, yend = 80 - finalY, color = colorOutcome),
geom_segment(aes(x = x, y = y,
xend = finalX, yend = finalY, color = colorOutcome),
lineend = "round", size = 1.5, arrow = arrow(length = unit(0.10, "inches")),
stat = "identity", position = "identity") +
facet_grid(~colorOutcome) +
labs(
color = "Outcome of Pass"
color = "Outcome of Pass",
caption = "Created using ggshakeR"
)
} else if (type == "all") {
plot <- plot +
geom_segment(aes(x = x, y = 80 - y,
xend = finalX, yend = 80 - finalY, color = colorOutcome),
geom_segment(aes(x = x, y = y,
xend = finalX, yend = finalY, color = colorOutcome),
lineend = "round", size = 1.5, arrow = arrow(length = unit(0.10, "inches")),
stat = "identity", position = "identity") +
labs(
color = "Outcome of Pass"
color = "Outcome of Pass",
caption = "Created using ggshakeR"
)
}
}
Expand Down
7 changes: 4 additions & 3 deletions R/plot_passflow.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ plot_passflow <- function(data, data_type = "statsbomb", binwidth = 0) {

if (nrow(PassFlow) > 0) {
plot <- plot +
geom_bin2d(data = data, aes(x = x, y = 80 - y), alpha = bin_alpha,
geom_bin2d(data = data, aes(x = x, y = y), alpha = bin_alpha,
binwidth = c(bin, bin), position = "identity") +
scale_fill_gradientn(colours = viridis_d_pal) +
geom_segment(aes(x = x, y = 80 - y, xend = finalX, yend = 80 - finalY, alpha = countPasses),
geom_segment(aes(x = x, y = y, xend = finalX, yend = finalY, alpha = countPasses),
color = "white", lineend = "round", size = 2, arrow = arrow(length = unit(0.08, "inches"))) +
labs(
fill = "Count of Passes Started",
alpha = "Number of Passes Made"
alpha = "Number of Passes Made",
caption = "Created using ggshakeR"
)
}
plot
Expand Down
12 changes: 6 additions & 6 deletions man/plot_pass.Rd

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

0 comments on commit 3293fce

Please sign in to comment.