I used "proc sgplot" to create a bar chart.
I want to complete it with a legend, but I can't find a solution.
I found examples for "proc gplot", but I don't want to change all of my work.
here is my code:
|proc sgplot data=Grafik;|
|Title "Stabdiagramm der Variable: Geschlecht";|
|yaxis label="Absolute Häufigkeit";|
|xaxis label="Geschlecht der Studierenden";|
|vbar Geschlecht / barwidth=0.3;|
Thank you for the support!
Yes you can, but I think you may need to use PROC TEMPLATE/PROC SGRENDER
Here is a code example:
define statgraph sgdesign;
entrytitle halign=center 'Type in your title...';
entryfootnote halign=left 'Type in your footnote...';
layout lattice / rowdatarange=data columndatarange=data rowgutter=10 columngutter=10;
barchart x=SEX y=HEIGHT / name='bar' clusterwidth=1.0 fillattrs=(color=CXA4A5A4 );
discretelegend 'bar' / opaque=false border=true halign=left valign=bottom displayclipped=true across=1 order=rowmajor location=inside;
proc sgrender data=SASHELP.CLASS template=sgdesign;
SGPLOT will automatically create a legend when needed. Since you don't have any group variable or other features, there is no need for a legend. However, if you want, you can add a KEYLEGEND statement of your own, with a reference to a name of the plot. Please see product documentation.
I'm not clear how you represent the "observation values" in your graph, but I have a couple of thoughts:
1. If each bar represents an observation, you can create a separate column containing the descriptions that lines up with your "Geschlecht" variable values and assign it to the GROUP option on the VBAR statement. The legend will automatically show the descriptions.
2. If it is additional information that does not line up with the bar chart, consider using an INSET statement to add the information to the chart.
Hope this helps!
thank you for the ideas !!
The INSET statement works well.
But I have a problem with the size of the bar chart-box. My description is too big for it.
Perhaps their is an alternative to make the bar chart-box greater or to put the description outside of the graph.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.