## Advanced Graphing Techniques Part 3 - Creating a Heat Map in R

It's been a long week and I am going to post the extent I have figured out on creating heatmaps in R.  Mainly just enough to completely confuse and frustrate myself.  I would love if some people from the community would help and chime in on the spots I having issues with.  I am hoping to create a graphic close to the following that Jeremy Greenhouse did at BaseballAnalysts.com: Here are the only 2 steps so far.

1. Follow steps 2 and 3 from Part 2 of the series to install R if you have not yet.
2. Here is the code that I am currently using to post in the console to create a heatmap.

This is a reference to a file on your computer.  It can be put anywhere.   I have set up a sample one to download that contain some values I just made up.  It needs to be in .csv format to work in R.

mysurface <- as.matrix(mysurface)

filled.contour(x=seq(from=-1.5,to=1.5,length=15),
y=seq(from=1,to=4,length=15),
z=mysurface,
nlevels=25,
axes=TRUE,
#col=hsv(h=seq(from=5/6,to=0,length=25),s=1,v=1),
color.palette=topo.colors
)

These lines are the heart of the map creation.   Here is a reference page for what each of the lines mean.  The two length values need to be set to the same value as the number of columns and rows in the .csv on your computer.

lines(c(.25,.25,-1.25,-1.25,.25),c(1.5,3.5,3.5,1.5,1.5), col=c("red"))

I have no idea why the box is offset this way.  One of several mysteries.  Here is what the final output should look like using the sample data provided:

-----------------------------------------------------------------

That is pretty simple, but it is not best analysis and here is where I need HELP.

1. It was impossible for me to create the map without using the .csv so far.  My main problem is creating a .sql output that is 15 rows by 15 columns.  If someone knows how to write a query that looks at one area and then moves to the next that would be great.  I have spent way too hours on this and right now I need to take a break.
2. I am trying to limit the programs needed to be known to R and SQL.  People may have seen some outputs like the following my brother created them, but used PHP to do the stepping through the strike zone.
3. I have also not be able to set the range values on the right part of the image.  If a new high or low value is introduced into the data, the data value scale changes.  I can't get it to be consistent from player to player.

Sorry for not having a 100% complete process, but I have to step away for a bit.  Hopefully one of our great readers can step in and help.  Thanks in advance.

Here the code again to just cut and paste into R:

mysurface <- as.matrix(mysurface)

filled.contour(x=seq(from=-1.5,to=1.5,length=15),
y=seq(from=1,to=4,length=15),
z=mysurface,
nlevels=25,
axes=TRUE,
#col=hsv(h=seq(from=5/6,to=0,length=25),s=1,v=1),
color.palette=topo.colors
)
x <- 10*1:nrow(mysurface)
y <- 10*1:ncol(mysurface)
contourLines(x, y, mysurface)
lines(c(.25,.25,-1.25,-1.25,.25),c(1.5,3.5,3.5,1.5,1.5), col=c("red"))