An Idea Exchange for SAS software and services

Comments
by Super Contributor
on ‎09-16-2013 03:42 PM

I just had to create a stacked bar and needed the labels to be centered.

SGPLOT wouldn't allow it.(or I am not aware of it)

So I went back to annotate/gchart - but needed to calculate the mid-point using the high/low values for each subgroup.

by SAS Super FREQ
on ‎09-16-2013 04:58 PM

Yes, this would be something we can add in future release.  It was considered that such labeling would lead to a cluttered graph, especially with short segments stacked closely.  However, if you need such a graph, and are willing to use Annotate, then there is an easier way with SGPLOT.

Summarize your data using proc MEANS by category and group.  Then, use data step to compute the low and high value for each stacked segment per category.  Now, use the HIGHLOW bar to plot each segment, and use a SCATTER with MarkerChar to overlay the labels.

I will work up an example and post in Graphically Speaking.

by Regular Contributor
on ‎09-16-2013 05:05 PM

Thanks! I've solved this for the current project by using a line plot to overlay a VBAR, actually (since Scatter can't overlay a bar graph).  If you set width=0, the line chart data labels overlay nicely, and if you are using GTL you can set the datalabelpos to data (center).  (This doesn't work in SGPLOT - I guess you haven't enabled that datalabel pos yet?)  Just would be nice to do it here (and to be able to work on unsummarized data). If you do enable it, I would definitely suggest some option for controlling when the label is shown for small bars.  Perhaps option of [show anyway]|[offset with line]|[suppress] or something like that.  I'm just removing the line segments for small bars myself.

by SAS Super FREQ
on ‎09-16-2013 05:36 PM

SGPLOT VLINE will work, except you can't control the the location of the data labels.  With GTL you can overlay scatter on bar chart and control label drawing but you need to set the right position.  I agree with your suggestion that we will need some kind of DataLabelPolicy to control how the labels are drawn.

by Regular Contributor
on ‎09-16-2013 05:47 PM

Hmm.  I just tried again with scatter with barchartparm (in GTL) and had no problems with it - very confused as I rather thought it was disallowed last time I tried.  I must have tried something else that was similar but not identical Smiley Happy  Perhaps it was in SGPANEL.  Thanks!

by SAS Employee RobertAllison_SAS
on ‎09-17-2013 08:30 AM

Note that with gchart annotate, rather than having to calculate the numeric coordinate of each stacked bar segment, you can just use the 'subgroup' variable, and gchart & annotate will figure it out for you Smiley Happy  Here's an example...

data my_anno; set sashelp.class;
xsys='2'; ysys='2'; when='a'; position='4';
midpoint=age; subgroup=trim(left(name));
function='label'; text=trim(left(name))||'a0'x;
run;

proc gchart data=sashelp.class anno=my_anno;
hbar age / discrete type=sum sumvar=height subgroup=name nolegend;
run;

foo.png

by Super Contributor
on ‎09-17-2013 08:43 AM

Thank you,Rob.

But not quite what I need.

The SGPLOT or the ANNOTATE/GCHART will allow for labels to be placed either above or below the line.

I needed centered labels

stacked_bar_labels.PNG

by SAS Employee RobertAllison_SAS
on ‎09-17-2013 09:05 AM

Ahh - ok ... that's a little more complicated, but can be done by sorting the data, and then "walking" your way through a data step, such as ...

proc sort data=sashelp.class out=my_anno;
by age name;
run;


data my_anno; set my_anno;
by age;
retain cumulative;
if first.age then cumulative=0;
cumulative+height;
midpoint=age; x=cumulative-(height/2); output;
run;


data my_anno; set my_anno;
xsys='2'; ysys='2'; when='a'; position='5';
function='label'; text=trim(left(name));
run;


proc gchart data=sashelp.class anno=my_anno;
hbar age / discrete type=sum sumvar=height subgroup=name nolegend;
run;

foo.png

by Super Contributor
on ‎09-17-2013 09:13 AM

Yes.

I did that Smiley Happy

I have already created the plot attached. I was merely mentioning that there is a bit of calculations (which I enjoy) to figure out the position.

Smiley Happy

by Regular Contributor
on ‎09-15-2014 02:24 PM

Is there a way to mark this idea completed or something like that? 

This was added in SAS 9.4 M2.  Thanks Sanjay and team for being responsive and adding this useful feature!

Idea Statuses
Top Liked Authors