Data visualization with SAS programming

Help with boxplot clipping

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Help with boxplot clipping


Hi,

Since ODS graphics boxplot does not support clipping, I'm trying to manually calculate the clipping range and change the boxplot template to add an overlay plot for the clipping range. I'm following the formula here: http://support.sas.com/documentation/cdl/en/statug/63347/HTML/default/viewer.htm#statug_boxplot_sect.... With the data provided in the example, the clipping ranges I computed seem to match the values on the plot. But when I used a different data set, I got very different results. Here's my code. Can someone help me understand what's causing the difference? Thanks!

proc sort data=sashelp.cars out=cars(keep=make horsepower);

     by make;

     where make < 'L';

run;

/* ============== manual calculation ==================*/

proc means data=cars q1 q3 nway missing noprint;

     class make;

     var horsepower;

     output out=cars_sum (drop=_freq_ _type_) q1=grp_q1 q3=grp_q3;

run;

proc means data=cars_sum mean nway missing noprint;

      var grp_q1 grp_q3;

      output out=all_sum(drop=_freq_ _type_) mean=grp_q1_mean grp_q3_mean;

run;

data all_sum;

     set all_sum;

     ymax = grp_q1_mean + (grp_q3_mean - grp_q1_mean) * 1.5;

     ymin = grp_q3_mean - (grp_q3_mean - grp_q1_mean) * 1.5;

run;

/* =================== get results from boxplot ============== */

ods graphics off;

proc boxplot data=cars;

     plot horsepower * make /clipfactor=1.5;

run;


Accepted Solutions
Solution
‎10-30-2012 11:07 AM
SAS Employee
Posts: 5

Re: Help with boxplot clipping

Hi Xian,

You are doing the calculations correctly.  The clipping range only determines which data points are ignored for vertical axis scaling.  After the clipping range is established, PROC BOXPLOT scales the vertical axis to provide "nice" tick mark values.  So the clipped data range from 149 to 308 results in tick marks from 100 to 350 by 50.  You can use the PLOT statement VAXIS= option to specify a tighter range, for example 140 to 310 by 10.

View solution in original post


All Replies
Super Contributor
Posts: 543

Re: Help with boxplot clipping

Hi Xian,

I ran your code in SAS 9.3 and it ran great.

I added a couple of options as I saw in the reference you suggested.

proc boxplot data=cars;

     plot horsepower * make /
       clipfactor=1.5
       clipsymbol = dot
       cliplegpos  = top
       cliplegend  = '# Clipped Boxes'
       clipsubchar = '#'
    ;
run;

Are you using SAS 9.2 or 9.3?

One thing I noticed, and your code already has it, it was the ODS Graphics OFF statement, which I did not have initially (But you did).

Not sure what to say.

If this doesn't work for you, you could use proc gplot with the symbol i = boxNN options where "NN" will tell SAS what percent of data to display...eeeh, say i = Box20 will have the lower bound at 20 percentile and the high bound at 80 percentile.

Best of luck!

Attachment
SAS Employee
Posts: 963

Re: Help with boxplot clipping

Xian - are you intentionally turning ods graphics off for a specific reason?

If you turn ods graphics off, I believe Proc Boxplot reverts back to a "pre- ods graphics" boxplot (which is created using the SAS/Graph graphics libraries, I believe -- Proc Boxplot is not a SAS/GRAPH proc per-say, but prior to ods graphics there was some "overlap").  Probably the best way to go, for the most flexibility & functionality for Proc Boxplot, is to use ods graphics.

Occasional Contributor
Posts: 9

Re: Help with boxplot clipping

I actually wanted to turn graphics on. But if I turn it on, the clipping will not work. What I don't understand is that the clipping range proc boxplot gave me is different from what I got from manual calculation. For this example. The boxplot shows clipping range of 100 - 350. But I got a clipping range of 149 - 308. Is my manual calculation wrong?

Solution
‎10-30-2012 11:07 AM
SAS Employee
Posts: 5

Re: Help with boxplot clipping

Hi Xian,

You are doing the calculations correctly.  The clipping range only determines which data points are ignored for vertical axis scaling.  After the clipping range is established, PROC BOXPLOT scales the vertical axis to provide "nice" tick mark values.  So the clipped data range from 149 to 308 results in tick marks from 100 to 350 by 50.  You can use the PLOT statement VAXIS= option to specify a tighter range, for example 140 to 310 by 10.

Occasional Contributor
Posts: 9

Re: Help with boxplot clipping

Thanks BuckyRansdell! You are correct. The ymax and ymin are not the final ymax and ymin on the chart. They are determined by SAS auto scaling algorithm.

Post a Question
Discussion Stats
  • 5 replies
  • 479 views
  • 0 likes
  • 4 in conversation