Data visualization with SAS programming

Modifying the datalabel position in SGplot to be directly above the marker

Accepted Solution Solved
Reply
Contributor AD
Contributor
Posts: 31
Accepted Solution

Modifying the datalabel position in SGplot to be directly above the marker

I have a plot where I would like the datalabels to be positioned directly above the marker. In the code below, sometimes the datalabel is directly above the marker, but most of the time the default seems to be to have the datalabel off to the side of the marker. How can I do this?

Data Cand1;
Input Domain $ Items PercM SE LowerL UpperH;
Cards;
D1 44 86 80 78 94
D2 30 73 10 63 83
D3 38 79 90 70 88
D4 13 69 13 56 82
;
Run;

Proc sort data=cand1;
by descending domain;
run;

proc sgplot data= Cand1 noautolegend nocycleattrs  ;
  scatter y=Domain x=PercM / datalabel=PercM datalabelattrs=(size=12 weight=bold) markerattrs=(symbol=plus);
  scatter y=Domain x=LowerL /  datalabel=LowerL datalabelattrs=(size=9) markerattrs=(symbol=plus)  ;
  scatter y=Domain x=UpperH /  datalabel=UpperH datalabelattrs=(size=9) markerattrs=(symbol=plus)  ;
  highlow y=Domain low=LowerL high=UpperH  ;
  xaxis label="Percent Correct" values=(0,20,40,60,80,100) ;
  yaxis label="Domain" discreteorder=data;
run;


Accepted Solutions
Solution
‎01-15-2015 01:58 PM
SAS Super FREQ
Posts: 925

Re: Modifying the datalabel position in SGplot to be directly above the marker

Unfortunately, the DATALABELPOS option for SCATTER plots was not surface in the SG procedures until SAS 9.4. However, The code below is a neat alternative. Basically, it plots the labels separately from the scatter points. The DISCRETEOFFSET option offsets the "labeling" scatter plot to raise the labels above the scatter points. The FORMAT is required to trim the excess space and center the labels.

proc sgplot data= Cand1 noautolegend nocycleattrs;

  format PercM LowerL UpperH 2.;

  scatter y=Domain x=PercM / markerattrs=(symbol=plus);

  scatter y=Domain x=LowerL /  markerattrs=(symbol=plus)  ;

  scatter y=Domain x=UpperH /  markerattrs=(symbol=plus)  ;

  scatter y=Domain x=PercM / markerchar=PercM markercharattrs=(size=12 weight=bold) discreteoffset=0.12;

  scatter y=Domain x=LowerL /  markerchar=LowerL markercharattrs=(size=9) discreteoffset=0.12;

  scatter y=Domain x=UpperH /  markerchar=UpperH markercharattrs=(size=9) discreteoffset=0.12;

  highlow y=Domain low=LowerL high=UpperH  ;

  xaxis label="Percent Correct" values=(0,20,40,60,80,100) ;

  yaxis label="Domain" discreteorder=data;

run;

View solution in original post


All Replies
SAS Super FREQ
Posts: 925

Re: Modifying the datalabel position in SGplot to be directly above the marker

What version of SAS are you using?

Contributor AD
Contributor
Posts: 31

Re: Modifying the datalabel position in SGplot to be directly above the marker

According to Proc Product_status (I'm using EG), the SAS version is 9.3_M1

Solution
‎01-15-2015 01:58 PM
SAS Super FREQ
Posts: 925

Re: Modifying the datalabel position in SGplot to be directly above the marker

Unfortunately, the DATALABELPOS option for SCATTER plots was not surface in the SG procedures until SAS 9.4. However, The code below is a neat alternative. Basically, it plots the labels separately from the scatter points. The DISCRETEOFFSET option offsets the "labeling" scatter plot to raise the labels above the scatter points. The FORMAT is required to trim the excess space and center the labels.

proc sgplot data= Cand1 noautolegend nocycleattrs;

  format PercM LowerL UpperH 2.;

  scatter y=Domain x=PercM / markerattrs=(symbol=plus);

  scatter y=Domain x=LowerL /  markerattrs=(symbol=plus)  ;

  scatter y=Domain x=UpperH /  markerattrs=(symbol=plus)  ;

  scatter y=Domain x=PercM / markerchar=PercM markercharattrs=(size=12 weight=bold) discreteoffset=0.12;

  scatter y=Domain x=LowerL /  markerchar=LowerL markercharattrs=(size=9) discreteoffset=0.12;

  scatter y=Domain x=UpperH /  markerchar=UpperH markercharattrs=(size=9) discreteoffset=0.12;

  highlow y=Domain low=LowerL high=UpperH  ;

  xaxis label="Percent Correct" values=(0,20,40,60,80,100) ;

  yaxis label="Domain" discreteorder=data;

run;

Contributor AD
Contributor
Posts: 31

Re: Modifying the datalabel position in SGplot to be directly above the marker

Awesome, thanks! Works perfectly.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 2067 views
  • 0 likes
  • 2 in conversation