BookmarkSubscribeRSS Feed
A_Kh
Barite | Level 11

Hello Community, 

I'm trying to create a plot with a custom legend using proc sgplot. Below is the result template that I need to achieve.
Using sashelp.cars dataset I created an example plot that has a legend. Need to customize it to make it similar to the template legend. 
Any guidance would be much appreciated!

template.
Picture1.png
code. 

proc sgplot data=sashelp.cars;
	hbox invoice/  category=type  group=type GROUPORDER=ascending;
	yaxis discreteorder=data; 
	keylegend/title='Approximate Meaningful Score Regions' location=OUTSIDE POSITION=bottom;
run;

cars.png

4 REPLIES 4
JeffMeyers
Barite | Level 11

Does the legend have to be below the x-axis?  And if so does it have to be done with SGPLOT?  An option you could use would be to add a BLOCK plot and set the position to the bottom so it goes along the x-axis.  You would have to add the x-values and text values you would want to use to your plot dataset.

 

If it has to be below the x-axis I would say you would then have to switch to using GTL and make a 2 row one column lattice layout.   You could use the same method to make a block plot, but have it be in the second overlay cell (bottom row of the lattice).  You would want to sync up the x-axis.  Something like the following:

proc template;
   define statgraph boxplots;
   begingraph;


   layout lattice / rows=2 columns=1 columndatarange=union;
       layout overlay / yaxisopts=(set your y axis options in here) 
                      xaxisopts=(Set your x-axis options in here);
          /**box plot code stuff**/
       endlayout;
       layout overlay / xaxisopts=(Set your same x-axis options in here) yaxisopts=(display=none);
           /**block plot code stuff**/
        endlayout;
     endlayout;
     endgraph;
    end;
run;
proc sgrender data=YourData template=boxplots;
run;
            
A_Kh
Barite | Level 11

@JeffMeyers , 

Thank you for your input!
Yes, legend should be as close as possible to the template legend, below x-axis. Proc choice is flexible as long as it gets the job done. Sgplot is my default proc for rarely created figures. Tried block plot, but didn't get the desired result. Couldn't make the GTL way work neither.. Would it be possible for you to showcase how to create the desired legend using  the above example? 

Ksharp
Super User

Here is an example:

 

data have;
 set sashelp.heart;
run;
proc sort data=have;
by descending bp_status;
run;




/*Make a dataset for customize legend*/
data legend;
input _id :$20. _bp_status $ value;
text_value=cum_value+value/2;
cum_value+value;
cards;
BloodStatus Optimal  30
BloodStatus Normal   30
BloodStatus High     40
;
run;
data want;
 set have legend;
run;

proc template;
define statgraph y2axis ;
begingraph;
layout lattice /  rows=2 rowgutter=0 rowweights=(0.8 0.2) ; 

layout overlay/ walldisplay=none YAXISOPTS=(display=(label tickvalues)) XAXISOPTS=(LINEAROPTS=(VIEWMAX=250)) ;
boxplot x=bp_status y=weight/orient=horizontal group=bp_status 
 OUTLINEATTRS=(color=black) OUTLIERATTRS=(size=0) WHISKERATTRS=(color=black)
 MEANATTRS=(size=0) MEDIANATTRS=(color=black);
endlayout;

layout overlay/walldisplay=none YAXISOPTS=(display=(tickvalues)) XAXISOPTS=(display=(ticks tickvalues));
BARCHARTPARM CATEGORY=_id response=value/group=_bp_status orient=horizontal
 OUTLINEATTRS=(thickness=0) BASELINEATTRS=(thickness=0) GROUPORDER=DATA BARWIDTH=1;
TEXTPLOT x=text_value y=_id text=_bp_status /TEXTATTRS=(color=black size=10) POSITION=left CONTRIBUTEOFFSETS=none;
endlayout;



endlayout;
endgraph;
end;
run;



data dattrmap;
  input id $ value $ fillcolor :$20. ;
datalines;
myid High    CXDEEBF7 
myid Normal  CX9ECAE1 
myid Optimal CX3182BD
;
run;
proc sgrender data=want template=y2axis dattrmap=dattrmap;
dattrvar bp_status="myid";
dattrvar _bp_status="myid";
run;

Ksharp_0-1758187379827.png

 

A_Kh
Barite | Level 11

Hi @Ksharp , 

Thank you for the example, yes, this is the legend I need!
I'm trying to apply this example in my data, but still cannot get the legend plots aligned with the boxplots by tick values and colors. As seen in the template, tick markers are exactly the same both in the main plot and the legend, legend categories aligned with respective box plots above.
In your example, how we can align the legend regions with Weight regions (boxes, tick markers and values) in the main plot?

In my case, regions should be marked approximately as follwing:

  • The “None” region is defined as the range from 0 to 6
  • The “Mild” region is defined as the range from 7 to 11
  • The “Moderate” region is defined as the range from 12 to 18
  • The “Severe” region is defined as the range from 19 to 23
  • The “Very severe” region is defined as the range from 24 to 30



cars.png


hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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