Data visualization with SAS programming

how to set a global legend for several subplots within one contour plot

Reply
Occasional Contributor
Posts: 8

how to set a global legend for several subplots within one contour plot

[ Edited ]

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

SAS Super FREQ
Posts: 890

Re: color related with specific numeric in colormodel= option and a global legend used for all cells

What version of SAS are you using?

Occasional Contributor
Posts: 8

Re: color related with specific numeric in colormodel= option and a global legend used for all cells

Hello, thank you. I use sas9.4

SAS Super FREQ
Posts: 3,415

Re: how to set a global legend for several subplots within one contour plot

[ Edited ]

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.

Occasional Contributor
Posts: 8

Re: how to set a global legend for several subplots within one contour plot

Hi Rick,

 

It sounds a brilliant idea, I'll try it. It should work. Thank you very much!

 

Regards,

Tingting

SAS Super FREQ
Posts: 890

Re: how to set a global legend for several subplots within one contour plot

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;
SAS Super FREQ
Posts: 890

Re: how to set a global legend for several subplots within one contour plot

[ Edited ]

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;

 

Ask a Question
Discussion stats
  • 6 replies
  • 274 views
  • 3 likes
  • 3 in conversation