Learn R Programming

mlbplotR

The code for this package was copied heavily from nflplotR with minor changes to support Major League Baseball logos.

The goal of mlbplotR is to provide functions and geoms that help with visualizations of MLB related analysis. It provides ggplot2 geoms that do the heavy lifting of plotting MLB logos in high quality, with correct aspect ratio, and possible transparency.

Installation

The easiest way to get mlbplotR is to install it from CRAN with:

install.packages("mlbplotR")

To get a bug fix or use a feature from the development version, you can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("camdenk/mlbplotR")

Usage

Let’s plot every team on a grid with some extra customization:

library(mlbplotR)
library(ggplot2)
library(dplyr)

teams_colors_logos <- mlbplotR::load_mlb_teams() |> 
  dplyr::filter(!team_abbr %in% c("AL", "NL", "MLB")) |> 
  dplyr::mutate(
    a = rep(1:6, 5),
    b = sort(rep(1:5, 6), decreasing = TRUE)
  )


 ggplot2::ggplot(teams_colors_logos, aes(x = a, y = b)) +
   mlbplotR::geom_mlb_logos(aes(team_abbr = team_abbr), width = 0.075) +
   ggplot2::geom_label(aes(label = team_abbr), nudge_y = -0.35, alpha = 0.5) +
   ggplot2::theme_void() 

There is a getting started guide that will walk you through more use cases for the package.

Contributing

  • Open an issue if you’d like to request specific data or report a bug/error.

Copy Link

Version

Install

install.packages('mlbplotR')

Monthly Downloads

245

Version

1.2.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Camden Kay

Last Published

September 2nd, 2025

Functions in mlbplotR (1.2.0)

load_headshots

Load Player ID Map With Headshot Info
mlb_team_factor

Create Ordered MLB Team Factor
mlb_team_tiers

Create MLB Team Tiers
gt_mlb_column_labels

Replace Team Abbreviations/Player IDs With Images In Column Labels
mlb_player_tiers

Create MLB Player Tiers
load_mlb_teams

Load MLB Team Colors, and Logos
load_milb_teams

Load MiLB Team Colors, and Logos
gt_mlb_headshots

Render Player Headshots in 'gt' Tables
gt_mlb

Add MLB team logos into rows of a gt table
gt_milb

Add MiLB team logos into rows of a gt table
scale_mlb

Scales for MLB Team Colors
theme_mlb

Theme for MLB Team Logos
GeomMLBdarkcaplogo

mlbplotR: Create 'ggplot2' and 'gt' Visuals with Major League Baseball Logos
scale_axes_mlb

Axis Scales for MLB Team Logos
valid_team_names

Output Valid MLB Team Abbreviations
geom_mlb_headshots

ggplot2 Layer for Visualizing MLB/MiLB Player Headshots
element

Theme Elements for Image Grobs
geom_milb_logos

ggplot2 Layer for Visualizing MiLB Team Logos
geom_cap_logos

ggplot2 Layer for Visualizing MLB Team Cap Logos
gt_merge_stack_team_color

Merge and stack text from two columns in gt and color one with team colors
ggpreview

Preview ggplot in Specified Dimensions
geom_lines

ggplot2 Layer for Horizontal and Vertical Reference Lines
geom_mlb_logos

ggplot2 Layer for Visualizing MLB Team Logos
clean_team_abbrs

Standardize MLB Team Abbreviations
geom_from_path

ggplot2 Layer for Visualizing Images from URLs or Local Paths