A few days ago, I posted an article on displaying first N bars from a data set. This is useful when the data is sorted by descending response, and only the first few values are significant. There were a few interesting comments, including one that was regarding the treatment of long category values. That comment motivated me to write up this article on ways to deal with graphs having long category values.
When we have a bar chart with long category values, the SGPLOT default x-axis behavior is to split the tick values on white space to create a nice readable graph. However, sometimes the text string can include long words without breaks. In such a case, the x-axis displays the values at a 45 degree angle as shown below. For this article, I generated some random long labels (max 40 characters) with white spaces.
In such a case we can use an HBAR, to display the categories on the y-axis. This often results in a better graph, with long category values displayed on the y-axis.
The y-axis tick values do not split automatically even when white space is present in the text strings. However, one can use the FITPOLICY=SPLIT option on the y-axis to split the values to the y-axis uses about 25% of the width of the graph. Note, extra space is not automatically created to fit the wrapped text without overlapping. So, one has to customize the graph by reducing font size, or increasing the graph height.
Another way to address this issue is by dropping the y-axis tick values entirely, and using bar labels to display the category values. This could be more efficient in using the space, especially if the longest bar does not have the longest label. In the graph below, I have disabled the display o f the y-axis values, and displayed the same using the DATALABEL option.
Finally, we come to a new feature that will be released with SAS 9.40M5 SGPLOT procedure and GTL. This new option requests the graph to preferably place the data labels inside the bar if they fit. Using this option, we get the graph below. I have also sorted the bars by descending response.
The first four bars are able to fit the bar label inside the bar. So, additional space does not need to be reserved for these longer bars. As the bars get shorter, some of the data labels that do not fit inside the bar are displayed outside the bar to the right.
This new option is only available only for the HBAR. This may allow us to reduce the width needed for some graphs. This option is provided to manage the space used by data labels, and it is not necessary to display the categories as data labels. I will document this option once SAS 9.40M5 is released.
Full SGPLOT code: Long_Cat_Values