library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.4.3
## Warning: package 'ggplot2' was built under R version 4.4.3
## Warning: package 'tidyr' was built under R version 4.4.2
## Warning: package 'purrr' was built under R version 4.4.3
## Warning: package 'dplyr' was built under R version 4.4.2
## Warning: package 'forcats' was built under R version 4.4.2
## Warning: package 'lubridate' was built under R version 4.4.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   4.0.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.2.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## Warning: package 'plotly' was built under R version 4.4.3
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
horse <- read.csv("C:/Users/User/Downloads/2016 Triple Crown Nominations - Nominations.csv")
# Count trainer and location combinations
heat_data <- horse %>%
  count(TRAINER, LOC, name = "horse_count")
#Keep top trainers so it stays readable
top_trainers <- horse %>%
  count(TRAINER, sort = TRUE) %>%
  slice_head(n = 15)
heat_data <- heat_data %>%
  filter(TRAINER %in% top_trainers$TRAINER)
#Make interactive heatmap
p <- ggplot(heat_data, aes(x = LOC, 
                           y = reorder(TRAINER, horse_count),
                           fill = horse_count,
                           text = paste(
                             "Trainer:", TRAINER,
                             "<br>Location:", LOC,
                             "<br>Number of Horses:", horse_count))) +
  geom_tile(color = "black") +
  scale_fill_gradient(low = "lightblue", high = "darkblue") +
  labs(title = "Heatmap of Triple Crown Nomination",
       x = "Trainer",
       y = "Horse Count") +
  theme_minimal()
#Makes Interative
ggplotly(p,tooltip = "text")