Data visualization with SAS programming

Customizing the width of hbar sgplot

Reply
Contributor
Posts: 30

Customizing the width of hbar sgplot

I have plotted hbar chart with sgplot for different customers. The problem I have is if the plot has more than 1 bar the widths are okay but if the plot outputs only one bar the width becomes double. Is there any way I can make the width fixed no matter the number of bars created. I have already used the hbar statement barwidth=0.8 but this is not helping. I will appreciate any help. Thanks in advance

SAS Super FREQ
Posts: 1,184

Re: Customizing the width of hbar sgplot

There is no way to set a fixed bar width that will apply to all cases.  SG computes such things outside in, not inside out.  When there is only one category, the computations are a bit different.  We can look into it.

 

However, If you know you have a one category situation, you can try setting the offsetmin and offsetmax on the category axis to reduce the bar width.  yaxis offsetmin=0.3 offsetmax=0.3;

Contributor
Posts: 30

Re: Customizing the width of hbar sgplot

[ Edited ]
Posted in reply to Sanjay_SAS

Okay, thanks a lot. I will try that and let you know the outcome

I have the offsetmin and offsetmax set to 0.05 does that have any influence on the width of the bars?

yaxis display=(nolabel) offsetmin=0.05 offsetmax=0.05;

 

Contributor
Posts: 30

Re: Customizing the width of hbar sgplot

Hello Sanjay,

sorry I was on holidays. I came back today and tried your suggestion but it didn't work. Is there any other way I can adjust the width?

I will be glad if any one can help.

Am just thinking of using if statement. To say if number of bars less than 4 then width=? else width =?

but I haven't tried that yet.

SAS Super FREQ
Posts: 1,184

Re: Customizing the width of hbar sgplot

If you share your program with sample data, it is easier for others to help.  "Did not work" does not provide much information.

Contributor
Posts: 30

Re: Customizing the width of hbar sgplot

[ Edited ]
Posted in reply to Sanjay_SAS

Hi Sanjay;

sorry for repling so late. I was a little bit busy with some other issues. Here is the code for Program. The problem is that the width decreases with the number of bars. I do not want that, I want the width to be fix no matter the number of bars. I hope you can help.

Thanks in advance

proc import datafile= "saslibrary\test1.xls" out=test
dbms=xls replace;
getnames=yes;
run;
%macro report ();
%do i = 501 %to 505 ;

proc sgplot data=test  pad=(bottom=5%);
styleattrs datacolors=(red);

yaxis grid type=discrete discreteorder=data;
 
hbar sport_typ /response=count
                   barwidth=0.8 missing datalabel = count;
      
yaxis display=(nolabel) offsetmin=0.05 offsetmax=0.05;
xaxis label='Number of Athletes' grid offsetmin=0.0 offsetmax=0.05;
where rep_id = &i;

 run;
title " ";

%end;

%mend report;
%report ();
PROC Star
Posts: 854

Re: Customizing the width of hbar sgplot

A few things. Like in many other SAS programs, you do not need a macro. Sort your data by rep_id and use a BY statement in PROC SGPLOT instead as in this small example

 

proc sort data=sashelp.class;
	by sex;
run;

proc sgplot data=sashelp.class;
	by sex;
	scatter x=height y=weight;
run;

I don't think it is possible to fix a bar width, so it does not change with the number of bars in your plot. As the documentation states, the BARWIDTH statement "specifies the width of the bars as a ratio of the maximum possible width".

 

Contributor
Posts: 30

Re: Customizing the width of hbar sgplot

ok thanks for your reply

SAS Super FREQ
Posts: 1,184

Re: Customizing the width of hbar sgplot

Please see my previous answer to this question.  "There is no way to set a fixed bar width that will apply to all cases.  SG computes such things outside in, not inside out."  For a set graph size, the bar width will depend on the number of bars in the graph.  More bars -> less bar width.

Contributor
Posts: 30

Re: Customizing the width of hbar sgplot

Posted in reply to Sanjay_SAS

okay Sanjay, thanks for that

Ask a Question
Discussion stats
  • 9 replies
  • 185 views
  • 0 likes
  • 3 in conversation