Data & Packages

library(tidyverse)
library(ggrepel) 
library(viridisLite)
mlb_batting <- read_csv('baseballref.csv')
mlb_standings <- read_csv("mlbstandings22.csv")
mlb <- mlb_batting %>% 
  left_join(mlb_standings, by = 'Tm')

Plot 1

mlb %>% 
  ggplot() +
  geom_point(aes(x = BA, y = HR, size = W, color = Tm),  alpha = .8) +
  geom_point(aes(x = BA, y = HR, size = W), color = "black", shape = 21, aplha = .15) +
  geom_text_repel(aes(x = BA, y = HR, size = W, label = Tm), size = 3) +
  scale_size(guide = 'none') +
  scale_y_continuous(breaks = c(100, 120, 140, 160, 180, 200, 220, 240, 260)) +
  labs(x = "Batting Average", y = "Home Runs", title = "MLB Batting Average vs Home Runs", color = "Team") +
  guides(color = FALSE) +
  scale_color_viridis_d(option = "magma")

Edit Data

mlb_H_prop = mlb %>%
  rename(Double = '2B', Triple = '3B') %>% 
  mutate(Single = H - Double - Triple - HR ) %>% 
  mutate(Singles_prop = Single / H,
         Doubles_prop = Double / H,
         Triples_prop = Triple / H,
         HR_prop = HR / H) %>% 
  select(Tm, Singles_prop, Doubles_prop, Triples_prop, HR_prop, Division)

mlb_long2 <- mlb_H_prop %>%
  pivot_longer(cols = c(Singles_prop, Doubles_prop, Triples_prop, HR_prop), names_to = "stat", values_to = "value")

Plot 2

mlb_long2 %>% 
  select(Tm, stat, value, Division) %>%
  filter(Division == "AL Central") %>% 
  ggplot(aes(x = Tm, y = value, fill = stat)) +
  geom_col() +
  coord_polar(theta = "y") +
  scale_fill_viridis_d(name = 'Hit Type', option = 'magma') +
  scale_x_discrete(limits = c(" "," "," ", "Cleveland Guardians", "Chicago White Sox", "Detroit Tigers","Kansas City Royals","Minnesota Twins")) +
  theme_void() +
  theme(legend.position = "right") +
  geom_text(aes(label = paste0(round(value* 100), "%")),
            position = position_stack(vjust = .5), size = 3.5, color = "darkgrey", fontface = 'bold') + 
  geom_text(data = mlb_long2 %>% 
              select(Tm, stat, value, Division) %>%
              filter(Division == "AL Central" & !duplicated(Tm)),
            aes(label = Tm), position = position_stack(vjust = 0.75), size = 3, color = "Black", fontface = 'bold') +
  labs(title = "Proportions of Singles, Doubles, Triples, and Homeruns for the AL Central", fill = "Hit Type") +
  scale_fill_manual(values = c('#000004','#fcfdbf', '#b73779','#51127c'),
                    labels = c("Doubles", "Home Runs", "Singles", "Triples"))
## Scale for 'fill' is already present. Adding another scale for 'fill', which
## will replace the existing scale.