0

Below is R code (dataset included) that aims to replicate the style of this heatmap using a stat_desnity_2d plot. enter image description here

The code runs, but the code is ignoring my fill variable (titled "exitspeed" in the code) therefore the plot is showing only one color. Below is a picture. enter image description here

Does anyone know how to get a stat_density_2d plot to stop ignoring the fill variable (exitspeed) so that the output looks similar to the picture in the paragraph above? Thanks in advance!

Here is the dataset:

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='ID', 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)

    ggplot(kZone, aes(x,y)) +
      stat_density_2d(data=df, aes(x=s, y=h, fill=es),geom="polygon") +
      scale_fill_distiller(palette = "Spectral") +
      geom_path(lwd=1.5, col="black") +
      coord_fixed()
alistaire
  • 42,459
  • 4
  • 77
  • 117
Nate Walker
  • 217
  • 2
  • 12
  • 1
    What portion of the code does this data correspond to? It would be helpful if you could pare the code down to just what's needed for this problem and what uses the data you've included – camille Aug 04 '18 at 18:21
  • You can get something with `ggplot(df, aes(platelocside, platelocheight, fill = ..level..)) + stat_density_2d(geom = 'polygon')`, but it's only using two variables. To insert a third, you'd need everything within a level to be at the same value for that variable. An alternative is to bin ahead and use `geom_raster`. – alistaire Aug 04 '18 at 18:31
  • @camille sorry, it refers to the ggplot section of the code. The picture in my question plots platelocside and platelocheight and I need to figure out to include an exitspeed fill. Thanks for taking a look! – Nate Walker Aug 04 '18 at 18:58
  • @alistaire Thanks for the advice! Quick follow-up question. What do you mean when you say "everything within a level to be at the same value for that variable?" – Nate Walker Aug 04 '18 at 18:59
  • 2
    `stat_density_2d` calls `MASS::kde2d` and bins the result into levels. You're trying to fill those levels with a variable that has multiple values for the points within that level, which doesn't work. (Even if they were the same level, it'd still be a bit of work.) If you want to set the fill with a third variable, ultimately you don't actually want `stat_density_2d`, you want [`stat_contour`](https://community.rstudio.com/t/geom-contour-help/10607/2?u=alistaire). You'd need gridded data, though. – alistaire Aug 04 '18 at 19:27
  • @alistaire Gotcha, thanks for the clarification! – Nate Walker Aug 04 '18 at 20:14

0 Answers0