Data visualization with SAS programming

Overlay Square in Heatmap

Accepted Solution Solved
Reply
Contributor
Posts: 47
Accepted Solution

Overlay Square in Heatmap

Hi,

 

I'm trying to create a heat map of a strike zone in baseball. The data contains the x and y values for where the ball crossed the plate, and also whether it was hit or not. I'd like to create a heat map showing the best areas of contact, along with overlaying the strike zone. 

 

Ideally, the strike zone would just be a box from coordinates (-1,1), (1,1), (-1,3), (1,3).

 

Here's the code for the heatmap, which doesn't include whether or not contact was made:

 

proc sgplot data=coded_hits_num;
	heatmap x=plate_x y=plate_z / colormodel=(blue yellow red);
	run;

 

Thank you for the help.


Accepted Solutions
Solution
‎11-07-2017 01:36 PM
SAS Super FREQ
Posts: 4,124

Re: Overlay Square in Heatmap

I think I would merge the data with the coordinates of the corners of the strike zone and use a POLYGON statement to draw the rectangle, as follows:

 

data coded_hits_num;
do x = -1 to 1 by 0.1;
   do z = 1 to 3 by 0.1;
      p = 1 - (x**2 + (z-2)**2) / 4;
      hits = rand("Binomial", p, 100);
      output;
   end;
end;
run;

data strikeZone;
ID = "S";
input xPoly zPoly;
datalines;
-1 1
 1 1
 1 3
-1 3
;

data all;
merge coded_hits_num strikezone;
run;

proc sgplot data=all aspect=1 noautolegend;
	heatmap x=x y=z / freq=hits colormodel=(blue yellow red) nxbins=21 nybins=21;
   polygon ID=ID x=xPoly y=zPoly / lineattrs=(thickness=5);
   xaxis min=-2 max=2;
   yaxis min=0 max=4;
run;

SGPlot58.png

 

View solution in original post


All Replies
Super User
Super User
Posts: 9,227

Re: Overlay Square in Heatmap

This is the place to look: https://blogs.sas.com/content/graphicallyspeaking/ Has examples of every graph imaginable. A quick search shows plenty of overlay examples: https://blogs.sas.com/content/?s=overlay+shape
SAS Super FREQ
Posts: 496

Re: Overlay Square in Heatmap

Solution
‎11-07-2017 01:36 PM
SAS Super FREQ
Posts: 4,124

Re: Overlay Square in Heatmap

I think I would merge the data with the coordinates of the corners of the strike zone and use a POLYGON statement to draw the rectangle, as follows:

 

data coded_hits_num;
do x = -1 to 1 by 0.1;
   do z = 1 to 3 by 0.1;
      p = 1 - (x**2 + (z-2)**2) / 4;
      hits = rand("Binomial", p, 100);
      output;
   end;
end;
run;

data strikeZone;
ID = "S";
input xPoly zPoly;
datalines;
-1 1
 1 1
 1 3
-1 3
;

data all;
merge coded_hits_num strikezone;
run;

proc sgplot data=all aspect=1 noautolegend;
	heatmap x=x y=z / freq=hits colormodel=(blue yellow red) nxbins=21 nybins=21;
   polygon ID=ID x=xPoly y=zPoly / lineattrs=(thickness=5);
   xaxis min=-2 max=2;
   yaxis min=0 max=4;
run;

SGPlot58.png

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 171 views
  • 2 likes
  • 4 in conversation