1

I'm trying to create hitter heat maps that are dependent upon the average value of runs a certain hit is worth, plotted based on physical location of coordinates, ex. so that higher values of an area on the map are red, lower values are blue.

I've researched a bit and can't seem to come up with a solution for this.

EX plot locations + value (X is based on center of plate, to the left is negative, right is positive in meters):

Plot Location

For each of these plot points, there is an attached value (i.e some are 0.5, 0.25, 0, -0.5).

Is there a way to plot that buckets these values and averages them out to create a heatmap similar to this: Heat Map

I don't need it to be continuous like that necessarily, I'm fine with bucketing them. I know how to create the plot with the strike zone built in through ggplot and plot each point individually, but I'm struggling to find a way to aggregate based on nearby plot points and transform that into a heat meap.

My first post here, apologies if I am not up to standard with the amount of detail provided, if there's any more relevant info I can include I'm happy to.

Thank you!

adbmke
  • 11
  • 1
  • Sounds like you are looking for the `ggplot2` library's hexagon bin heatmap. Check out the examples on this page https://ggplot2.tidyverse.org/reference/geom_hex.html – M.Viking Feb 23 '20 at 23:19
  • Hi adbmke. Welcome to StackOverflow! Your post could benefit from a bit more detail on what you already tried as well as example data and code. You could read the info about [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and how to give a [minimale reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! – dario Feb 23 '20 at 23:19
  • I used hexagon bin for pitch frequency, but the issue with that is I believe it's just a counting function? I.E. it doesn't aggregate for each hexagon just counts within it..is there a way to manipulate that function? – adbmke Feb 23 '20 at 23:40

1 Answers1

0

If you want to base your heatmap on the mean of the Value variable, use the stat_summary_hex() function.

The data are divided into bins defined by x and y, and then the values of z in each cell is are summarized with fun.

The default "summary" function is mean(z).

The code below makes a heatmap plot using the mean of depth.

ggplot(diamonds, aes(x=carat, y=price, z=depth)) + 
   stat_summary_hex()

fun argument is the function for summary, the default function can be overridden with any function. For example max().

ggplot(diamonds, aes(x=carat, y=price, z=depth)) + 
   stat_summary_hex(fun = "max")
   #stat_summary_hex(fun = function(a) max(a)) #alternative for complex functions

https://ggplot2.tidyverse.org/reference/stat_summary.html

M.Viking
  • 5,067
  • 4
  • 17
  • 33