{"id":20942457,"url":"https://github.com/bdilday/geommlbstadiums","last_synced_at":"2025-08-22T00:07:20.259Z","repository":{"id":78136130,"uuid":"144206147","full_name":"bdilday/GeomMLBStadiums","owner":"bdilday","description":"Geoms to draw MLB stadiums in ggplot2","archived":false,"fork":false,"pushed_at":"2023-09-22T00:59:33.000Z","size":1945,"stargazers_count":61,"open_issues_count":2,"forks_count":2,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-12T03:43:25.668Z","etag":null,"topics":["baseball","ggplot2-geoms","r"],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bdilday.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-08-09T21:39:36.000Z","updated_at":"2025-02-17T22:12:33.000Z","dependencies_parsed_at":"2024-01-07T04:03:11.059Z","dependency_job_id":"dcdf22b0-a606-40d3-ba51-a8de507337f7","html_url":"https://github.com/bdilday/GeomMLBStadiums","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdilday%2FGeomMLBStadiums","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdilday%2FGeomMLBStadiums/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdilday%2FGeomMLBStadiums/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdilday%2FGeomMLBStadiums/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bdilday","download_url":"https://codeload.github.com/bdilday/GeomMLBStadiums/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254043218,"owners_count":22004912,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["baseball","ggplot2-geoms","r"],"created_at":"2024-11-18T23:27:11.551Z","updated_at":"2025-05-13T23:32:52.838Z","avatar_url":"https://github.com/bdilday.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, echo = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n#  fig.path = \"README-\",\n  fig.height = 6,\n  fig.width = 6,\n  fig.units = \"in\"\n)\n```\n\n\n# GeomMLBStadiums\n\nThis package defines a couple of Geoms to draw MLB stadiums in ggplot2. It also provides a Geom to draw a \"spraychart\" - `x` and `y` locations of batted balls with a stadium overlay. \n\n## Example use\n\n### Install from github and load the necessary libraries\n\n``` {r echo=FALSE, message=FALSE, warning=FALSE}\nlibrary(GeomMLBStadiums)\nlibrary(ggplot2)\nlibrary(dplyr)\n```\n\n``` {r eval=FALSE, message=FALSE}\ndevtools::install_github(\"bdilday/GeomMLBStadiums\")\nlibrary(GeomMLBStadiums)\nlibrary(ggplot2)\nlibrary(dplyr)\n```\n\n### The stadium data\n\nWhen you load the `GeomMLBStadiums` package it will attach the stadium paths as a data frame, `MLBStadiumsPathData`\n\n``` {r}\nhead(MLBStadiumsPathData)\n```\n\nThe data comprise the 30 current MLB stadiums, in addition to a \"generic\" stadium. The stadia are identified by team name, with the following conventions\n\n``` {r}\nunique(MLBStadiumsPathData$team)\n```\n\nThe segments are split up into `outfield_outer`, `outfield_inner`, `infield_inner`, `infield_outer`, `foul_lines`, and `home_plate`\n\n``` {r}\nunique(MLBStadiumsPathData$segment)\n```\n\n### Coordinates\n\nThe stadium paths are in the system of the `hc_x` and `hc_y` coordinates of MLBAM. These are inverted (because they're based on a display device where `y=0` is at top, IIRC) which means by default the field gets displayed upside down. This package provides a helper function, `mlbam_xy_transformation`, that transforms these values to a system where y increases from bottom to top and home plate is located at `(0, 0)`.\n\n``` {r}\nset.seed(101)\nbatted_ball_data = data.frame(hc_x = rnorm(20, 125, 10), \n                              hc_y = rnorm(20, 100, 20))\n\nhead(batted_ball_data)\n\nhead(mlbam_xy_transformation(batted_ball_data))\n\nsummary(mlbam_xy_transformation(batted_ball_data))\n\n```\n\n### `geom_mlb_stadium`\n\nThis uses `geom_mlb_stadium`, which implicitly loads the `MLBStadiumsPathData` data, to plot the 30 current stadiums.\n\n``` {r}\nggplot() + \n  geom_mlb_stadium(stadium_ids = \"all_mlb\", \n                   stadium_segments = \"all\") + \n  facet_wrap(~team) + \n  coord_fixed() + \n  theme_void()\n```\n\nAn alternative way is to explicitly pass the data to `geom_path`.\n\n``` {r}\nMLBStadiumsPathData %\u003e% \n  filter(team != 'generic') %\u003e% \n  mutate(g=paste(team, segment, sep=\"_\")) %\u003e% \n  ggplot(aes(x, y)) + \n  geom_path(aes(group=g)) + \n  facet_wrap(~team) + \n  coord_fixed() + \n  theme_void()\n```\n\nThis shows the generic stadium, which is the default,\n\n``` {r}\nggplot() + \n  geom_mlb_stadium(stadium_segments = \"all\") + \n  facet_wrap(~team) + \n  coord_fixed() + \n  theme_void()\n```\n\n### `geom_spraychart`\n\nThis generates some simulated data.\n\n``` {r}\n# first generate the data\nset.seed(101)\nbatted_ball_data = data.frame(hc_x = rnorm(20, 125, 10),\n                              hc_y = rnorm(20, 100, 20))\nbatted_ball_data$team = rep(c(\"angels\", \"yankees\"), each=10)\n```\n\nThis plots the data as a spraychart. By default it uses the \"generic\" stadium.\n\n``` {r}\nbatted_ball_data %\u003e% \n  ggplot(aes(x=hc_x, y=hc_y)) + \n  geom_spraychart() \n```\n\nAdd some styling using `theme_void` and `coord_fixed`\n\n``` {r}\nbatted_ball_data %\u003e% \n  ggplot(aes(x=hc_x, y=hc_y)) + \n  geom_spraychart() + \n  theme_void() + \n  coord_fixed()\n```\n\nThis transforms the data and the stadium before plotting, passes the team names in `stadium_ids`, draws all segments, and facets by field.\n\n``` {r}\nbatted_ball_data %\u003e% mlbam_xy_transformation() %\u003e%  \n    ggplot(aes(x=hc_x_, y=hc_y_, color=team)) + \n    geom_spraychart(stadium_ids = unique(batted_ball_data$team),\n                    stadium_transform_coords = TRUE, \n                    stadium_segments = \"all\") + \n    theme_void() + \n    coord_fixed() + \n    facet_wrap(~team) + \n    theme(legend.position = \"bottom\")\n```\n\nYou can make use of any of the other `ggplot2` functions, for example, contours from `stat_density2d`. The `mapping` argument for `geom_spraychart` gets passed to the underlying `geom_point`, as do any extra parameters passed into the `...` argument of `geom_spraychart`, e.g. `size=5` in the below.\n\n``` {r}\nbatted_ball_data %\u003e% mlbam_xy_transformation() %\u003e%  \n  ggplot(aes(x=hc_x_, y=hc_y_, color=team)) + \n  geom_spraychart(mapping = aes(shape=team), \n                  stadium_ids = unique(batted_ball_data$team),\n                  stadium_transform_coords = TRUE, \n                  stadium_segments = \"all\", size=5) + \n  theme_void() + \n  coord_fixed() + \n  facet_wrap(~team) + \n  theme(legend.position = \"bottom\") + \n  stat_density2d(color='gray')\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbdilday%2Fgeommlbstadiums","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbdilday%2Fgeommlbstadiums","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbdilday%2Fgeommlbstadiums/lists"}