Data visualization with SAS programming

Can I create a legend in "proc sgplot" ?

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

Can I create a legend in "proc sgplot" ?

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


Accepted Solutions
Solution
‎09-10-2013 01:53 PM
SAS Super FREQ
Posts: 890

Re: Can I create a legend in "proc sgplot" ?

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


All Replies
Super Contributor
Posts: 543

Re: Can I create a legend in "proc sgplot" ?

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;

SAS Super FREQ
Posts: 1,081

Re: Can I create a legend in "proc sgplot" ?


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.

Contributor
Posts: 29

Re: Can I create a legend in "proc sgplot" ?

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

SAS Super FREQ
Posts: 890

Re: Can I create a legend in "proc sgplot" ?

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

Contributor
Posts: 29

Re: Can I create a legend in "proc sgplot" ?

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

Solution
‎09-10-2013 01:53 PM
SAS Super FREQ
Posts: 890

Re: Can I create a legend in "proc sgplot" ?

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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