Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
10,180 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
^README\.Rmd$ | ||
^README-.*\.png$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Package: GeomMLBStadiums | ||
Title: GeomMLBStadiums: Draw Major League Baseball Stadiums with ggplot2 | ||
Version: 0.0.0.9000 | ||
Authors@R: person("Ben", "Dilday", email = "ben.dilday.phd@gmail.com.com", role = c("aut", "cre")) | ||
Description: Draw Major Leagure Stadiums in the ggplot2 framework | ||
Depends: R (>= 3.5.1) | ||
License: MIT + file LICENSE | ||
Encoding: UTF-8 | ||
LazyData: true | ||
Imports: ggplot2, | ||
grid | ||
RoxygenNote: 6.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Version: 1.0 | ||
|
||
RestoreWorkspace: No | ||
SaveWorkspace: No | ||
AlwaysSaveHistory: Default | ||
|
||
EnableCodeIndexing: Yes | ||
UseSpacesForTab: Yes | ||
NumSpacesForTab: 2 | ||
Encoding: UTF-8 | ||
|
||
RnwWeave: Sweave | ||
LaTeX: pdfLaTeX | ||
|
||
AutoAppendNewline: Yes | ||
StripTrailingWhitespace: Yes | ||
|
||
BuildType: Package | ||
PackageUseDevtools: Yes | ||
PackageInstallArgs: --no-multiarch --with-keep.source | ||
PackageRoxygenize: rd,collate,namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,2 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Ben Dilday | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
YEAR: 2018 | ||
COPYRIGHT HOLDER: Ben Dilday |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(geom_mlb_stadium) | ||
export(geom_spraychart) | ||
export(mlbam_xy_transformation) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
|
||
#' MLB Spraychart | ||
#' | ||
#' This generates a spraychart - a scatter plot of \code{x, y} values | ||
#' overlayed on a baseball field. It effectively overrides | ||
#' \code{geom_point} and additionally concatenates a \code{geom_mlb_stadium} layer. | ||
#' | ||
#' @seealso | ||
#' \code{\link{geom_mlb_stadium}} | ||
#' | ||
#' @param mapping Set of aesthetic mappings created by \code{\link{aes}} or | ||
#' \code{\link{aes_}}. If specified and \code{inherit.aes = TRUE} (the | ||
#' default), it is combined with the default mapping at the top level of the | ||
#' plot. You must supply \code{mapping} if there is no plot mapping. | ||
#' @param data The data to be displayed in this layer. There are three | ||
#' options: | ||
#' | ||
#' If \code{NULL}, the default, the data is inherited from the plot | ||
#' data as specified in the call to \code{\link{ggplot}}. | ||
#' | ||
#' A \code{data.frame}, or other object, will override the plot | ||
#' data. All objects will be fortified to produce a data frame. See | ||
#' \code{\link{fortify}} for which variables will be created. | ||
#' | ||
#' A \code{function} will be called with a single argument, | ||
#' the plot data. The return value must be a \code{data.frame.}, and | ||
#' will be used as the layer data. | ||
#' @param stat The statistical transformation to use on the data for this | ||
#' layer, as a string. | ||
#' @param position Position adjustment, either as a string, or the result of | ||
#' a call to a position adjustment function. | ||
#' @param na.rm If \code{FALSE}, the default, missing values are removed with | ||
#' a warning. If \code{TRUE}, missing values are silently removed. | ||
#' @param show.legend logical. Should this layer be included in the legends? | ||
#' \code{NA}, the default, includes if any aesthetics are mapped. | ||
#' \code{FALSE} never includes, and \code{TRUE} always includes. | ||
#' @param inherit.aes If \code{FALSE}, overrides the default aesthetics, | ||
#' rather than combining with them. This is most useful for helper functions | ||
#' that define both data and aesthetics and shouldn't inherit behavior from | ||
#' the default plot specification, e.g. \code{\link{borders}}. | ||
#' @param stadium_aes mapping to pass to \code{geom_mlb_stadium} | ||
#' @param stadium_ids character Vector of stadium ids. | ||
#' @param stadium_segments character Vector of \code{stadium_segments} to display. Options are | ||
#' 'outfield_inner', 'outfield_outer', 'infield', 'all'. | ||
#' @param stadium_transform_coords boolean if \code{TRUE}, then transform the path data | ||
#' according to \code{mlb_xy_transformation} | ||
#' @param ... other arguments passed on to \code{\link{layer}}. These are | ||
#' often aesthetics, used to set an aesthetic to a fixed value, like | ||
#' \code{color = "red"} or \code{size = 3}. They may also be parameters | ||
#' to the paired geom/stat. | ||
#' @export | ||
#' @examples | ||
#' g = ggplot() + geom_mlb_ | ||
#' set.seed(101) | ||
#' batted_ball_data = data.frame(hc_x = rnorm(20, 125, 10), hc_y=rnorm(20, 100, 20)) | ||
#' batted_ball_data$team = rep(c("angels", "yankees"), each=10) | ||
#' g <- ggplot(batted_ball_data) + geom_spraychart() | ||
#' | ||
geom_spraychart = function(mapping = NULL, data = NULL, stat = "identity", | ||
position = "identity", na.rm = FALSE, show.legend = NA, | ||
inherit.aes = TRUE, | ||
stadium_aes = NULL, | ||
stadium_ids = NULL, | ||
stadium_segments = "outfield_outer", | ||
stadium_transform_coords=FALSE, | ||
...) { | ||
|
||
list( | ||
geom_point(mapping = mapping, data = data, stat = stat, | ||
position = position, na.rm = na.rm, show.legend = show.legend, ...), | ||
|
||
geom_mlb_stadium(mapping = stadium_aes, | ||
stadium_transform_coords = stadium_transform_coords, | ||
stadium_ids = stadium_ids, | ||
stadium_segments = stadium_segments) | ||
) | ||
|
||
|
||
} | ||
|
||
#' Geom MLB Stadium | ||
#' | ||
#' This draws the outline of an MLB stadium. | ||
#' | ||
#' The data are based on the SVG files used by MLBAM. The SVG files have been | ||
#' converted to a \code{data.frame} for use with \code{ggplot2::geom_path} | ||
#' | ||
#' @seealso | ||
#' \code{\link{geom_spraychart}} | ||
#' | ||
#' @param mapping Set of aesthetic mappings created by \code{\link{aes}} or | ||
#' \code{\link{aes_}}. If specified and \code{inherit.aes = TRUE} (the | ||
#' default), it is combined with the default mapping at the top level of the | ||
#' plot. You must supply \code{mapping} if there is no plot mapping. | ||
#' @param data The data to be displayed in this layer. There are three | ||
#' options: | ||
#' | ||
#' If \code{NULL}, the default, the data is inherited from the plot | ||
#' data as specified in the call to \code{\link{ggplot}}. | ||
#' | ||
#' A \code{data.frame}, or other object, will override the plot | ||
#' data. All objects will be fortified to produce a data frame. See | ||
#' \code{\link{fortify}} for which variables will be created. | ||
#' | ||
#' A \code{function} will be called with a single argument, | ||
#' the plot data. The return value must be a \code{data.frame.}, and | ||
#' will be used as the layer data. | ||
#' @param stat The statistical transformation to use on the data for this | ||
#' layer, as a string. | ||
#' @param position Position adjustment, either as a string, or the result of | ||
#' a call to a position adjustment function. | ||
#' @param na.rm If \code{FALSE}, the default, missing values are removed with | ||
#' a warning. If \code{TRUE}, missing values are silently removed. | ||
#' @param show.legend logical. Should this layer be included in the legends? | ||
#' \code{NA}, the default, includes if any aesthetics are mapped. | ||
#' \code{FALSE} never includes, and \code{TRUE} always includes. | ||
#' @param inherit.aes If \code{FALSE}, overrides the default aesthetics, | ||
#' rather than combining with them. This is most useful for helper functions | ||
#' that define both data and aesthetics and shouldn't inherit behavior from | ||
#' the default plot specification, e.g. \code{\link{borders}}. | ||
#' @param stadium_ids mapping to pass to `If \code{TRUE}, the default, will check that | ||
#' supplied parameters and aesthetics are understood by the \code{geom} or | ||
#' \code{stat}. Use \code{FALSE} to suppress the checks. | ||
#' @param params Additional parameters to the \code{geom} and \code{stat}. | ||
#' @export | ||
#' @examples | ||
#' set.seed(101) | ||
#' batted_ball_data = data.frame(hc_x = rnorm(20, 125, 10), hc_y=rnorm(20, 100, 20)) | ||
#' batted_ball_data$team = rep(c("angels", "yankees"), each=10) | ||
#' g <- ggplot(batted_ball_data) + geom_spraychart(aes(x=hc_x, y=hc_y), stadium_ids = "angels") | ||
#' | ||
geom_mlb_stadium = function(mapping = NULL, data = NULL, stat = "identity", | ||
position = "identity", na.rm = FALSE, show.legend = NA, | ||
inherit.aes = FALSE, | ||
stadium_ids = NULL, | ||
stadium_segments = "outfield_outer", | ||
stadium_transform_coords=FALSE, | ||
...) { | ||
|
||
mapping = aes(x=x, y=y, group=segment, ...) | ||
data = GeomMLBStadiums::MLBStadiumsPathData | ||
#data = read.csv("inst/extdata/mlb_stadia_paths.csv", stringsAsFactors = FALSE) | ||
|
||
if (is.null(stadium_ids)) { | ||
stadium_ids = "generic" | ||
} | ||
|
||
data = | ||
do.call(rbind.data.frame, | ||
lapply(stadium_ids, function(s) { | ||
data[ (data$team == s),] | ||
}) | ||
) | ||
|
||
if ("all" %in% stadium_segments) { | ||
# noop | ||
} else if (!is.null(stadium_segments)) { | ||
data = do.call(rbind.data.frame, lapply(stadium_segments, function(s) { | ||
data[ (data$segment == s),] | ||
}) | ||
) | ||
} | ||
|
||
if (stadium_transform_coords) { | ||
data = mlbam_xy_transformation(data, x="x", y="y", column_suffix="") | ||
} | ||
|
||
layer( | ||
geom = GeomPath, mapping = mapping, data = data, stat = stat, | ||
position = position, show.legend = show.legend, inherit.aes = inherit.aes, | ||
params = list(na.rm = na.rm, ...) | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#' MLBAM X,Y transformation | ||
#' | ||
#' Transforms data in the \code{hc_x, hc_y} coordinates provided by | ||
#' MLBAM. The resulting coordinate system is in units of feet, and | ||
#' has home plate as \code{x=0, y=0} and the \code{y} coordinate | ||
#' increases toward the pitcher's mound. | ||
#' | ||
#' The data are transformed according to | ||
#' x -> scale * (x - 125) ; | ||
#' y -> scale * (199 - y) | ||
#' | ||
#' @param data a \code{data.frame} containing \code{x, y} coordinates | ||
#' @param x a character containing the name of the \code{x} coordinate | ||
#' @param y a character containing the name of the \code{y} coordinate | ||
#' @param column_suffix character. The transformed data will be named according | ||
#' to the \code{x, y} names, with \code{column_suffix} appended. | ||
#' @param scale numeric The scale to multiply the transformed data by. The default | ||
#' sets pixel 51 and sets it to the y-location of second base, \code{90 x sqrt{2}} | ||
#' @return \code{data.frame}. A copy of the input \code{data.frame} with the | ||
#' transformed data appended | ||
#' @export | ||
#' @examples | ||
#' set.seed(101) | ||
#' batted_ball_data = data.frame(hc_x = rnorm(20, 125, 10), hc_y=rnorm(20, 100, 20)) | ||
#' # append transformed x, y data | ||
#' transformed_data1 = mlbam_xy_transformation(batted_ball_data) | ||
#' # overwrite the x, y data with transformed values | ||
#' transformed_data2 = mlbam_xy_transformation(batted_ball_data, column_suffix="") | ||
mlbam_xy_transformation = function(data, | ||
x="hc_x", y="hc_y", | ||
column_suffix="_", | ||
scale=2.495671) { | ||
data[,paste0(x, column_suffix)] = scale * (data[,x] - 125) | ||
data[,paste0(y, column_suffix)] = scale * (199 - data[,y]) | ||
data | ||
} |
Oops, something went wrong.