One of the key benefits of using a horizontal bar chart is the ability to display statistics for each bar. This is a popular feature for the HBAR statement with the SAS/GRAPH GCHART procedure. So, let us review the options available to us to create such graphs using SGPLOT.
The simplest case is to display the frequency of each bar on the right hand side as shown in the graph on the right. Here we have used the SGPLOT HBAR statement with the DataLabel option with Position=right.
I have also used the NoWall, NoBorder options and suppressed axis lines and baseline to get this popular view. Note, the stat values are not colored by group. Click on the graph for a higher resolution image.
proc sgplot data=cars nowall noborder; hbar type / group=origin groupdisplay=cluster dataskin=pressed baselineattrs=(thickness=0) datalabel datalabelpos=right; yaxis display=(nolabel noline noticks); xaxis display=(noline noticks) grid; run;
In this example, I have included the Mean City and Highway mileage along with the frequency counts. Note, the frequency count values are now color coded by group. All values are displayed right justified in the column by default.
proc sgplot data=cars nowall noborder; label mpg_city='Mean City Mileage' mpg_highway='Mean Highway Mileage' n='Count'; format mpg_city mpg_highway 4.1; hbar type / group=origin groupdisplay=cluster stat=pct dataskin=pressed baselineattrs=(thickness=0); yaxistable n / stat=sum classdisplay=cluster colorgroup=origin valueattrs=(size=6 weight=bold) nostatlabel; yaxistable mpg_city mpg_highway/ stat=mean classdisplay=cluster colorgroup=origin valueattrs=(size=6 weight=bold); yaxis display=(nolabel noline noticks); xaxis display=(noline noticks) grid; run;
In the graph and code above, I have used one YAxisTable to display the frequency values by using an additional variable called "N" with Freq=Sum. This variable contains only "1" for each observation so we get the sum of the counts in this column. You can also use any other numeric variable with Stat=Freq, and set the variable label appropriately.
Using the YAxisTable instead of the DataLabel option as in the first graph allows us to color each observation by group. Then, I have used a second YAxisTable with mpg_city and mpg_highway as the variables with Stat=Mean to display the mean mileage values also colored by group.
For the graph on the right, I have used ValueHAlign=center to display each value in the center of the column using a 4.1 format. I have set the labels for the variables to indicate the statistic used for each label. I have also used faint horizontal bands for each category to help the eye across the graph.
Statistics can be displayed "Inside" or "Outside" the graph area, which is more apparent if graph borders are used. Additional statistics can be displayed by adding more variables to the YAxisTable statement, or using another YAxisTable statement to display values on the left of the bars.
Full Program: BarStats_SG_94