xshbj
xshbj

Reputation: 93

SAS-sgplot-add label to bar clusters

I am looking for an option to add label to bar clusters.

I want the values: 0.940, 0.250, 0.520, 0.580, and 0.230 in the middle of the clustered bars, like the following: enter image description here

Upvotes: 0

Views: 704

Answers (1)

Joe
Joe

Reputation: 63424

Assuming you have SAS 9.4 TS1M2 or later, you have an option, seglabel, which specifically does this, other than the 'display only once', which I don't think is a thing you can get out of the box.

proc sgplot data=sashelp.cars;
vbar cylinders/group=origin  groupdisplay=cluster seglabel;
run;

If you don't, or you need more control than that gives you (such as your request to display only once), there are options, such as in my paper, Labelling without the hassle; it is for stacked bar charts, but the general approach would work for clustered also, you'd just have to adjust things some.

This gets somewhat close, and probably would get nearly perfect for your case; it needs some customization for groups that don't have all three group values present, but you don't have that in your example.

More complex solutions exist using annotations or GTL, as well, with this general approach (of overlaying on the precomputed bars a label).

proc summary data=sashelp.cars;
class origin cylinders;
types origin*cylinders;
var mpg_city;
output out=cars_summary(drop=_:) n(mpg_city)=count;
run;

data cars_labels;
  set cars_summary;
  if origin='Asia' then count_asia=count;
  if origin='USA' then count_usa=count;
  if origin='Europe' then count_eu=count;
  
  ypos = floor(count/2);
run;

proc sgplot data=cars_labels;
vbarparm category=cylinders response=count/group=origin groupdisplay=cluster;
scatter x=cylinders y=ypos/markerchar=count_asia discreteoffset=-0.35;
scatter x=cylinders y=ypos/markerchar=count_usa discreteoffset=0.20;
scatter x=cylinders y=ypos/markerchar=count_eu discreteoffset=-0.1;

run;

Upvotes: 1

Related Questions