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.
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;
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;
@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?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.