Data visualization with SAS programming

data labels in each section of sgplot stacked bar

Reply
Occasional Contributor
Posts: 15

data labels in each section of sgplot stacked bar

Is there a command to put data labels in each bar of a chart?  This is what I have:

    proc sgplot data=figure2;
   yaxis label=' ' discreteorder=data valueATTRS=(family='calibri' size=8pt style=normal);
   xaxis discreteorder=data label = ' ' valueATTRS=(family='calibri' size=8pt style=normal);
   vbar group / response = col1 group = name grouporder=data barwidth=0.4 datalabel=col1 datalabelpos=data;
   keylegend / noborder location=outside position=top across=2 valueATTRS=(family='calibri' size=7pt style=normal);
   run;

Using this code gives me the value from the first segment of the stacked bar, at the top of the bar.  Using datalabel by itself gives the grand total at the top.

I need each segment to have it's own label, and some segments are too small for the value, so it would need to be off to the side.  Is there a way to do this?

Thanks


barchart_sample.JPG
Occasional Contributor
Posts: 15

Re: data labels in each section of sgplot stacked bar

This doesn't do what the bosses requested, but at least the data is available on the chart:

proc sgplot data=figure2;

            yaxis label=' ' discreteorder=data valueattrs=(family='calibri' size=8pt style=normal) ;

            xaxis discreteorder=data label = ' ' valueattrs=(family='calibri' size=8pt style=normal) ;

            vbar group / response = col1 group = name grouporder=data barwidth=0.4 datalabel=col1 datalabelpos=bottom

                datalabelattrs=(family='calibri' size=6pt style=normal);

            keylegend / noborder location=outside position=top across=2 valueattrs=(family='calibri' size=7pt style=normal);

        run;

It seems logical from the documentation that the datalabelpos=data option should label the data sections, it just doesn't.  I've seen other similar questions on various Communities boards but no easy answers.  Is Annotate the only option?  If anyone knows a great simple solution, I'd still like to see it.

SAS Super FREQ
Posts: 1,141

Re: data labels in each section of sgplot stacked bar

There are ways to do this, but not by using a simple option on VBAR or VBARPARM.  Sort the values by category and group, and compute the low, high values (and midpoint) for each bar segment per category yourself.  Then, use a HIGHLOW bar to draw the segments.  Use a SCATTER plot with MARKERCHAR to place the values at the midpoint of each segment.

Ask a Question
Discussion stats
  • 2 replies
  • 1322 views
  • 0 likes
  • 2 in conversation