Dear all,
I have a plot that looks like this:
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;
@Anita_n wrote:
Dear all,
I have a plot that looks like this:
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.
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
Hi all,
I will appreciate any suggestions to this topic
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?
@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?
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.
@GraphGuy so how can I made my graph wider?
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;
@DanH_sas ok, thanks I will try that
@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:
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?
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
@ghosh Thanks for that, I will try that right away
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!
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.