BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ammarhm
Lapis Lazuli | Level 10

Hi everyone, 

I am using sgplot to create heat-maps to present data form a table. 

It is a great functionally, but I am struggling with the colour scale for the heat map. 

Basically, i am generating several heat maps, based on data from a table, the min and max for the data in each heat map are slightly different, resulting in the colour scale (autogenerated by SGPLIT) being different between the different heat maps, and hence the user cannot appreciate the difference easily. 

TO illustrate this, see the following code:

* Generate a table with random numbers;

Data have;
	do a= 1 to 3;
		do i=1 to 5;
			L=1;
			o=floor(rand('Uniform')*10);
			output;
		end;
	output;
	end;


	do a= 1 to 3;
		do i=1 to 5;
			L=2;
			o=floor(rand('Uniform')*5)+2; 
			output;
		end;
	output;
	end;

run;


*Generate heat-maps;

proc sgplot data=have;
   heatmapparm x=a y=i colorresponse=o / outline discretex colormodel=ThreeColorRamp;
   text x=a y=i text=o / textattrs=(size=10pt) strip;
   by L ;
   gradlegend;
run;

you will notice that the code above generates two heat maps but that cells containing the same number (say 6) in the two heat maps have different colour shades.

Is there a way to (force) standardise the colour scale across the heat maps?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

How about this one ?

 

Data have;
call streaminit(12345678);
	do a= 1 to 3;
		do i=1 to 5;
			L=1;
			o=floor(rand('Uniform')*10);
			output;
		end;
	output;
	end;


	do a= 1 to 3;
		do i=1 to 5;
			L=2;
			o=floor(rand('Uniform')*5)+2; 
			output;
		end;
	output;
	end;

run;


proc sql noprint;
select min(o),max(o) into :min,:max from want;
quit;


proc template;
  define statgraph cal_heatmap;
    begingraph ;
	  entrytitle "xxxxxxxxxx";
	  rangeattrmap name="rmap";
        range &min- &max / rangecolormodel=ThreeColorRamp;		

	  endrangeattrmap;
	  rangeattrvar attrmap="rmap" attrvar=cvar var=o;
	  layout overlay ;
	    heatmapparm x=a y=i colorresponse=cvar/ name="heatmap" xgap=1 ygap=1;
		scatterplot x=a y=i/ MARKERCHARACTER=o  markerattrs=(size=20pt) ;

		*continuouslegend "heatmap" ;
	  endlayout;
	endgraph;
  end;
run;

proc sgrender data=have template=cal_heatmap;
by l;
run;

Ksharp_0-1632923961824.png

 

Ksharp_1-1632923980346.png

 

View solution in original post

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

Have you tried using the COLORGROUP variable?

See this and this examples.

ammarhm
Lapis Lazuli | Level 10

Thanks @ChrisNZ 

Very hepful indded. 

I only needed to read the documentation in more details, I found the rattrmap option seems to give control over how the colours are assigned. 

 

data myrattrmap;
retain id "myID";
length min $ 5 max $ 5;
input min $ max $ color $ altcolor $;
datalines;
_min_ 4    purple purple
4    6   gold   gold
6   _max_ green  green
;
run;

proc sgplot data=have rattrmap=myrattrmap;
   heatmapparm x=a y=i colorresponse=o / rattrid=myID outline discretex colormodel=ThreeColorRamp;
   text x=a y=i text=o / textattrs=(size=10pt) strip;
   by L ;
   gradlegend;
run;
Ksharp
Super User

How about this one ?

 

Data have;
call streaminit(12345678);
	do a= 1 to 3;
		do i=1 to 5;
			L=1;
			o=floor(rand('Uniform')*10);
			output;
		end;
	output;
	end;


	do a= 1 to 3;
		do i=1 to 5;
			L=2;
			o=floor(rand('Uniform')*5)+2; 
			output;
		end;
	output;
	end;

run;


proc sql noprint;
select min(o),max(o) into :min,:max from want;
quit;


proc template;
  define statgraph cal_heatmap;
    begingraph ;
	  entrytitle "xxxxxxxxxx";
	  rangeattrmap name="rmap";
        range &min- &max / rangecolormodel=ThreeColorRamp;		

	  endrangeattrmap;
	  rangeattrvar attrmap="rmap" attrvar=cvar var=o;
	  layout overlay ;
	    heatmapparm x=a y=i colorresponse=cvar/ name="heatmap" xgap=1 ygap=1;
		scatterplot x=a y=i/ MARKERCHARACTER=o  markerattrs=(size=20pt) ;

		*continuouslegend "heatmap" ;
	  endlayout;
	endgraph;
  end;
run;

proc sgrender data=have template=cal_heatmap;
by l;
run;

Ksharp_0-1632923961824.png

 

Ksharp_1-1632923980346.png

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 947 views
  • 4 likes
  • 3 in conversation