Data visualization with SAS programming

How to add a text box to chart in proc sgplot?

Reply
Occasional Contributor
Posts: 9

How to add a text box to chart in proc sgplot?

[ Edited ]

I'm plotting a histogram/scatterplot overlay of pre- vs. post-intervention program emergency room admission counts via the sgplot procedure.

 

I'm using the legend statement to add an inset text box containing the mean values of the # of pre- and post-intervention ED admits + the Wilcoxon signed-rank test p-value.

 

The proc sgplot code runs without error, however the text box is missing. What's missing from my code?

 

Here's a portion of the FreqOut dataset plotted by proc sgplot:

   

data FreqOut;
input Obs CNT_PRE_ED_BH pre_percent CNT_POST_ED_BH post_percent pre_proportion  post_proportion;
cards;
1 0 1.97183 0 45.3521 0.019718 0.45352
2 1 5.91549 1 9.0141 0.059155 0.09014
3 2 6.19718 2 9.8592 0.061972 0.09859
4 3 3.38028 3 5.0704 0.033803 0.05070
5 4 4.78873 4 3.9437 0.047887 0.03944
6 5 5.35211 5 4.2254 0.053521 0.04225
7 6 9.29577 6 3.0986 0.092958 0.03099
8 7 7.60563 7 4.5070 0.076056 0.04507
9 8 7.32394 8 3.0986 0.073239 0.03099
10 9 8.73239 9 1.1268 0.087324 0.01127
11 10 5.63380 10 1.6901 0.056338 0.01690
;
run;

 

 

 

Here's my code:

 

%let var_post = CNT_POST_ED_BH;
%let var_pre = CNT_PRE_ED_BH;

proc template;
  define style styles.defstyle;
  parent=styles.default;
    style body from body /
          background = pearl;
    class graphgridlines  /
          contrastcolor=white;
    class graphbackground /
          color=beige;
    class graphwalls /
          color=lightgrey;
  end;
run;

ods _all_ close;
ods html gtitle style=defstyle;

proc sgplot data=FreqOut;
	vbarparm category=&var_pre response=pre_proportion / legendlabel='Pre-intervention BH ED frequency';
   	scatter x=&var_post y=post_proportion / legendlabel='Post-intervention BH ED frequency'
      		markerattrs=GraphDataDefault(symbol=CIRCLEFILLED size=6);
	title "Pre-intervention vs. post-intervention BH ED visits where IS_SCC='Y' during CY2014";
	xaxis label="BH ED visit count";
	yaxis tickvalueformat=percent5. grid label="Percentage" values=(0 to .5 by .1);
	*inset 'Mean pre-intervention BH ED count = 10.13' 'Mean post-intervention BH ED count = 3.44' 'Wilcoxon signed-rank test p-value < .0001' / border position=topright;
	legend value=('Mean pre-intervention BH ED count = 10.13' 'Mean post-intervention BH ED count = 3.44' 'Wilcoxon signed-rank test p-value < .0001')
		position = (top right inside)
		cframe = white mode=protect;
run;

ods html close;
ods listing;

 

 

 

 

 

Thanks!

 

 

SAS Super FREQ
Posts: 925

Re: How to add a text box to chart in proc sgplot?

Instead of the LEGEND statement, you will want to use the INSET statement:

 

inset 'Mean pre-intervention BH ED count = 10.13' 'Mean post-intervention BH ED count = 3.44' 'Wilcoxon signed-rank test p-value < .0001' /
		position = topright;

 

The LEGEND statement is from SAS/Graph. Legends in the SG procedures are control via the KEYLEGEND and GRADLEGEND statements.

 

Hope this helps!

Dan 

Occasional Contributor
Posts: 9

Re: How to add a text box to chart in proc sgplot?

Thanks Dan,

 

Is there a way to edit the text in the inset statement, such as adding a border and a white background?

 

Robert

SAS Super FREQ
Posts: 3,615

Re: How to add a text box to chart in proc sgplot?

Here is a link to the documentation for the INSET statement. You can use the BORDER option to add a border. Unfortunately, INSET does not support an OPAQUE option the way that KEYLEGEND does.

 

If you absolutely require more control and are willing to experiment, you could look at the TEXT statement. Unfortunately, the TEXT statement requires that the text be in the data set, rather than specified in the procedure as for the INSET.

Occasional Contributor
Posts: 9

Re: How to add a text box to chart in proc sgplot?

The INSET statement will have to do for now. Maybe a future version of SAS EG will include more options for INSET?

 

Doesn't look like the TEXT statement is supported in SAS EG.

Super User
Posts: 11,101

Re: How to add a text box to chart in proc sgplot?

If you want more control of your graphs you may want to investigate the Graphics Template Language. There are a few more appearance options set with the INSETOPTS statement in LAYOUT DATALATTICE, LAYOUT DATAPANEL or SCATTERPLOTMATRIX to work with INSET.

SAS Super FREQ
Posts: 925

Re: How to add a text box to chart in proc sgplot?

Since you mentioned DATAPANEL and DATALATTICE, I thought I would mention the INSET statement has been added to PROC SGPANEL in SAS 9.4. The "required argument" specification for this statement is different in SGPANEL vs. SGPLOT, as the SGPANEL version uses variables to control the inset content per cell. See the documentation for more details.

 

Thanks!
Dan

Ask a Question
Discussion stats
  • 6 replies
  • 812 views
  • 0 likes
  • 4 in conversation