Hi,
I want to draw a contour plot with contourplotparm. The plot contains 4 cells and each cell shares the same x- and y- values but different z- values. Now here's the problem: I want to make all the cells share one legend, i.e. a same contour color in all 4 cells relates to a same z- value. Such as in the following example, I'd like to make z=0.0012 always related to orange across all the cells no matter z is density1 or density2 etc.
data gridded;
set sashelp.gridded;
density1=density*1.2;
density2=density*1.5;
density3=density*1.8;
run;
proc template;
define statgraph contourplotparm;
begingraph;
LAYOUT LATTICE / COLUMNS=2 ROWS=2 ;
layout overlay;
contourplotparm x=height y=weight z=density /
contourtype=fill nhint=12
name="Contour1" colormodel=threecolorramp;
continuouslegend "Contour1"/ title="K";
endlayout;
layout overlay;
contourplotparm x=height y=weight z=density1 /
contourtype=fill nhint=12
name="Contour2" colormodel=threecolorramp;
continuouslegend "Contour2"/ title="K";
endlayout;
layout overlay;
contourplotparm x=height y=weight z=density2 /
contourtype=fill nhint=12
name="Contour3" colormodel=threecolorramp;
continuouslegend "Contour3"/ title="K";
endlayout;
layout overlay;
contourplotparm x=height y=weight z=density3 /
contourtype=fill nhint=12
name="Contour4" colormodel=threecolorramp;
continuouslegend "Contour4"/ title="K";
endlayout;
endlayout;
endgraph;
end;
run;
proc sgrender data=gridded template=contourplotparm;
run;
Can anyone help? Thanks!
Regards,
Tingting
What version of SAS are you using?
Hello, thank you. I use sas9.4
I often find that it is helpful to add a fake observation that contains the minimum and maximum value of the response variables. This forces the color model to use the same scales. For your example:
data Fake;
height=.; weight=.;
/* set lower bound for response vars */
density1=0; density2=0; density3=0; output;
/* set upper bound for response vars */
density1=0.0035; density2=0.0035; density3=0.0035; output;
run;
data All;
set Fake gridded;
run;
Then use the "All" data set in your analysis.
For other ways that prepending fake data can help with graphics, see this article on prepending fake data.
Hi Rick,
It sounds a brilliant idea, I'll try it. It should work. Thank you very much!
Regards,
Tingting
A RANGEATTRMAP is probably the best solution for you. You will want to replace literal values for the "min" and "max" keywords below; otherwise, you ranges will not be consistent across all contours. Give the code below a try and see if it works for you.
proc template;
define statgraph contourplotparm;
begingraph;
rangeattrmap name="zcolor";
range min-<5 / rangecolormodel=(threecolorramp:startcolor threecolorramp:neutralcolor);
range 5-5 / rangecolor=orange;
range 5<-max / rangecolormodel=( threecolorramp:neutralcolor threecolorramp:endcolor);
endrangeattrmap;
rangeattrvar attrvar=zdensity var=density attrmap="zcolor";
rangeattrvar attrvar=zdensity1 var=density1 attrmap="zcolor";
rangeattrvar attrvar=zdensity2 var=density2 attrmap="zcolor";
rangeattrvar attrvar=zdensity3 var=density3 attrmap="zcolor";
LAYOUT LATTICE / COLUMNS=2 ROWS=2 ;
layout overlay;
contourplotparm x=height y=weight z=zdensity /
contourtype=fill nhint=12
name="Contour1" colormodel=threecolorramp;
continuouslegend "Contour1"/ title="K";
endlayout;
layout overlay;
contourplotparm x=height y=weight z=zdensity1 /
contourtype=fill nhint=12
name="Contour2" colormodel=threecolorramp;
continuouslegend "Contour2"/ title="K";
endlayout;
layout overlay;
contourplotparm x=height y=weight z=zdensity2 /
contourtype=fill nhint=12
name="Contour3" colormodel=threecolorramp;
continuouslegend "Contour3"/ title="K";
endlayout;
layout overlay;
contourplotparm x=height y=weight z=zdensity3 /
contourtype=fill nhint=12
name="Contour4" colormodel=threecolorramp;
continuouslegend "Contour4"/ title="K";
endlayout;
endlayout;
endgraph;
end;
run;
ods listing style=toto ;
proc sgrender data=gridded template=contourplotparm;
run;
One more change...
You wanted one legend for all contours. Now that you are using the rangeattrmap, you can assign the legend to just one plot. See the revised code below:
proc template;
define statgraph contourplotparm;
begingraph;
rangeattrmap name="zcolor";
range min-<5 / rangecolormodel=(threecolorramp:startcolor threecolorramp:neutralcolor);
range 5-5 / rangecolor=orange;
range 5<-max / rangecolormodel=( threecolorramp:neutralcolor threecolorramp:endcolor);
endrangeattrmap;
rangeattrvar attrvar=zdensity var=density attrmap="zcolor";
rangeattrvar attrvar=zdensity1 var=density1 attrmap="zcolor";
rangeattrvar attrvar=zdensity2 var=density2 attrmap="zcolor";
rangeattrvar attrvar=zdensity3 var=density3 attrmap="zcolor";
LAYOUT LATTICE / COLUMNS=2 ROWS=2 ;
layout overlay;
contourplotparm x=height y=weight z=zdensity /
contourtype=fill nhint=12
name="Contour1" colormodel=threecolorramp;
endlayout;
layout overlay;
contourplotparm x=height y=weight z=zdensity1 /
contourtype=fill nhint=12
name="Contour2" colormodel=threecolorramp;
endlayout;
layout overlay;
contourplotparm x=height y=weight z=zdensity2 /
contourtype=fill nhint=12
name="Contour3" colormodel=threecolorramp;
endlayout;
layout overlay;
contourplotparm x=height y=weight z=zdensity3 /
contourtype=fill nhint=12
name="Contour4" colormodel=threecolorramp;
endlayout;
sidebar / align=bottom;
continuouslegend "Contour4"/ title="K";
endsidebar;
endlayout;
endgraph;
end;
run;
ods listing style=toto ;
proc sgrender data=gridded template=contourplotparm;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.