BookmarkSubscribeRSS Feed
Anita_n
Pyrite | Level 9

Dear all, 

I have a plot that looks like this:

Anita_n_0-1616251564544.png

I wish to insert the percentages in the graph and the keylegend should just be one or two lines outside the graph. I used across=2 but it isn't working. Any help?

proc sgplot data = mydata dattrmap= myattrmap pad=(bottom=5%) noautolegend noborder;
xaxis grid type=discrete discreteorder=data;
vbar year / response=frequencies group = mygroup attrid=mygroup barwidth=0.9 missing ;
xaxis  display= (nolabel) valueattrs=(family="verdana" size=8pt) fitpolicy=rotate ValuesRotate=Vertical;
yaxis  display= (nolabel) valueattrs=(family="verdana" size=8pt);
keylegend / location=outside position=bottomright across=2 title="" valueattrs=(size=10 family="verdana") outerpad=(top=0.2cm) noborder;
run; 
12 REPLIES 12
ballardw
Super User

@Anita_n wrote:

Dear all, 

I have a plot that looks like this:

Anita_n_0-1616251564544.png

I wish to insert the percentages in the graph and the keylegend should just be one or two lines outside the graph. I used across=2 but it isn't working. Any help?

proc sgplot data = mydata dattrmap= myattrmap pad=(bottom=5%) noautolegend noborder;
xaxis grid type=discrete discreteorder=data;
vbar year / response=frequencies group = mygroup attrid=mygroup barwidth=0.9 missing ;
xaxis  display= (nolabel) valueattrs=(family="verdana" size=8pt) fitpolicy=rotate ValuesRotate=Vertical;
yaxis  display= (nolabel) valueattrs=(family="verdana" size=8pt);
keylegend / location=outside position=bottomright across=2 title="" valueattrs=(size=10 family="verdana") outerpad=(top=0.2cm) noborder;
run; 

The basic instruction to display a value on a VBAR is DATALABEL. If you have a variable that has the value you want then you use Datalabel=thatvariablename. If you do not supply a variable then it displays the value of the statistic summarized.

 

If you actually have such small values, the blue and pinkish areas of your graph, you may not space on the graph to cleanly display the results.

 

"Isn't working" is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "</>" icon to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "</>" icon or attached as text to show exactly what you have and that we can test code against.

 

Anita_n
Pyrite | Level 9

Thank you for your reply, How can I remove the spaces on graph please inorder to  cleanly display the results?

Here is my code and data 

 

proc sgplot data =test1 dattrmap=colors  pad=(bottom=5%) noautolegend noborder;
xaxis grid type=discrete discreteorder=data;
vbar year / response=rel_freq group=group_an  attrid=group_an barwidth=0.9 missing;
xaxis  display= (nolabel) valueattrs=(family="verdana" size=8pt) fitpolicy=rotate ValuesRotate=Vertical;
yaxis  display= (nolabel) valueattrs=(family="verdana" size=8pt);
keylegend / location=outside position=bottomright across=0 title="" 
valueattrs=(size=10 family="verdana") outerpad=(top=0.2cm) noborder;
run; 

I will also like to set the keylegend to just be a single row, to get more space for the graph. I used across=1 but it seems this is not working due to the length of the values. Am not sure anyway if am right. I will also like to display the no_cases and  percentages of each group in the corresponding bar.

 

I will be grateful for any help

 

 

 

 

 

 

 

Anita_n
Pyrite | Level 9

Hi all,

I will appreciate any suggestions to this topic

GraphGuy
Meteorite | Level 14

If I use the latest code you posted, and use keylegend / across=1 (rather than across=0), I get the legend in the bottom/right. Is this the legend you're wanting?

 

keylegend.png

Anita_n
Pyrite | Level 9

@GraphGuy : Thanks for your reply. I taught I could get that the group names on one line but this seem not to be possible due to the length of the group names. I think I will have to shorten the names to get the desired result.

@ballardw  mentioned that I could reduce the spaces on the graph to get neat results of the areas which have small values. I wish to know how to achieve this. I can only reduce the barwidth but what does he mean by reduce spaces on the graph to achieve neat results.

 

I also wish to show the values of the number of case in the bars. How can one do that in SAS?

GraphGuy
Meteorite | Level 14

Ahh! - so you want them in one row! (When you had mentioned across=2, I thought you were talking about columns.)

 

You could make your graph wider, or the legend text font smaller, or shorten the words/text of the legend.

 

Anita_n
Pyrite | Level 9

@GraphGuy so how can I made my graph wider?

DanH_sas
SAS Super FREQ

Use the WIDTH and HEIGHT options on the ODS GRAPHICS statement to control the size. For example, add the following statement before you run the SG procedure:

ods graphics / width=1000px height=480px;

 

Anita_n
Pyrite | Level 9

@DanH_sas  ok, thanks I will try that

Anita_n
Pyrite | Level 9

@DanH_sas Hey, thanks it worked. I have also found out that the bars can be labelled using seglabel option. when I used that I got the following results:

Anita_n_0-1616485294367.png

Is there anyway to display the values of the small sized areas (using for example arrows) ? I mean is there any option which add arrows to display such values?

 

ghosh
Barite | Level 11

You could use the sganno option for sgplot to annotate the small sections.  There are several papers that you can google for, check this out to begin with

 

https://support.sas.com/rnd/datavisualization/papers/Annotate_Your_SGPLOT_Graphs.pdf

Anita_n
Pyrite | Level 9

@ghosh Thanks for that, I will try that right away

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 12 replies
  • 1934 views
  • 2 likes
  • 5 in conversation