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

## Sandarizing colour scale in heatmap

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 ;
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
Super User

## Re: Sandarizing colour scale in heatmap

```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;

```

3 REPLIES 3
Tourmaline | Level 20

## Re: Sandarizing colour scale in heatmap

Have you tried using the COLORGROUP variable?

See this and this examples.

Lapis Lazuli | Level 10

## Re: Sandarizing colour scale in heatmap

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 ;
run;
``````
Super User

## Re: Sandarizing colour scale in heatmap

```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;

```

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