1

I am trying to create a heatmap with three variables that plots the x,y and uses z as a fill. Below is an example of a heatmap that I want to replicate. enter image description here

It was recommended to me that I try using stat_contour in ggplot2 to create this heatmap. However, for me to run this code, I need to transform my data frame into gridded data. Would anyone know how to transform a data frame into grided data so I can execute this code? Currently, when I run my code, I get this error and I think it is because my data is not in a grid format.

Error in FUN(X[[i]], ...) : object 'exitspeed' not found

Please see below for my R code and data set. Thanks in advance!

Data:

structure(list(platelocheight = c(2.594, 3.803, 3.254, 3.599, 
3.617, 3.297, 2.093, 3.611, 2.842, 3.316, 2.872, 3.228, 3.633, 
4.28, 3.309, 2.8, 2.632, 3.754, 2.207, 3.604, 3.443, 2.188, 3.452, 
2.553, 3.382, 3.067, 2.986, 2.785, 2.567, 3.804), platelocside = c(0.059, 
-1.596, -0.65, -0.782, -0.301, -0.104, 0.057, -0.807, 0.003, 
1.661, 0.088, -0.32, -1.115, -0.146, -0.364, -0.952, 0.254, 0.109, 
-0.671, -0.803, -0.212, -0.069, -0.09, -0.472, 0.434, 0.337, 
0.723, 0.508, -0.197, -0.635), exitspeed = c(69.891, 73.352, 
83.942, 85.67, 79.454, 85.277, 81.078, 73.573, 77.272, 59.263, 
97.343, 91.436, 76.264, 83.479, 47.576, 84.13, 60.475, 61.093, 
84.54, 69.959, 88.729, 88.019, 82.18, 83.684, 86.296, 90.605, 
79.945, 59.899, 62.522, 77.75)), .Names = c("platelocheight", 
"platelocside", "exitspeed"), row.names = c(NA, 30L), class = "data.frame")
> 

R Code:

library(RODBC)
library(ggplot2)


con=odbcConnect('username',uid='userid', pwd = 'password')

df=sqlQuery(con,"select platelocheight, platelocside, exitspeed from pitches_sample where pitchcall='InPlay' 
            and exitspeed is not null")

topKzone <- 3.5
botKzone <- 1.6
inKzone <- -0.95
outKzone <- 0.95
kZone <- data.frame(
  x=c(inKzone, inKzone, outKzone, outKzone, inKzone),
  y=c(botKzone, topKzone, topKzone, botKzone, botKzone)
)

df$h <- round(df$platelocheight)
df$s <- round(df$platelocside)
df$es <- round(df$exitspeed)


v <-ggplot(data=df, aes(s,h,z=exitspeed)) 
v+stat_contour()+
  geom_path(aes(x,y),data=kZone,lwd=2,col="black",alpha=.3) +
  coord_fixed()
Nate Walker
  • 217
  • 2
  • 12
  • 2
    For the error message you receive, I think you perhaps want to change `z=exitspeed` to `z = es`, maybe; you seem not to have `exitspeed` in `df`. – jazzurro Aug 05 '18 at 06:53
  • 2
    You also want to check [**this question**](https://stackoverflow.com/questions/19065290/r-ggplot-stat-contour-not-able-to-generate-contour-lines) and [**this question**](https://stackoverflow.com/questions/28330821/r-plot-filled-contour-output-in-ggpplot2) – jazzurro Aug 05 '18 at 06:56
  • @jazzurro Thanks for the advice, jazzurro! Unfortunately, when I change z to es, I am still getting the same error. I'll definitely check out those links you posted. Thanks! – Nate Walker Aug 05 '18 at 16:52
  • If you see the questions, I think you will realize what you need to do. Good luck. – jazzurro Aug 06 '18 at 02:32

0 Answers0