BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DaLack
Calcite | Level 5

Hallo,

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;
   run;

Thank you for the support!

Daniel

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

The best alternative for putting them outside the graph is to use multiple FOOTNOTE statements. To make the graph itself bigger, you can use the WIDTH and HEIGHT options on the ODS GRAPHICS statement.

Thanks!

Dan

View solution in original post

6 REPLIES 6
AncaTilea
Pyrite | Level 9

Hi.

Yes you can, but I think you may need to use PROC TEMPLATE/PROC SGRENDER

Here is a code example:

proc template;

define statgraph sgdesign;

begingraph;

   entrytitle halign=center 'Type in your title...';

   entryfootnote halign=left 'Type in your footnote...';

   layout lattice / rowdatarange=data columndatarange=data rowgutter=10 columngutter=10;

      layout overlay;

         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;

      endlayout;

   endlayout;

endgraph;

end;

run;

1111

proc sgrender data=SASHELP.CLASS template=sgdesign;

run;

Jay54
Meteorite | Level 14


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.

DaLack
Calcite | Level 5

Thank you for the support!

But I have the problem, that I used numbers for the obsvervation values of a nominal variable.
Now I want to add a "description", where I can explain, what each number means.

I look forward to the answer,

Daniel

DanH_sas
SAS Super FREQ

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!

Dan

DaLack
Calcite | Level 5

Hey Dan,

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.

Thanks,

Daniel

DanH_sas
SAS Super FREQ

The best alternative for putting them outside the graph is to use multiple FOOTNOTE statements. To make the graph itself bigger, you can use the WIDTH and HEIGHT options on the ODS GRAPHICS statement.

Thanks!

Dan

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2428 views
  • 6 likes
  • 4 in conversation