SAS SGPLOT VBOX:在箱线图上显示平均值和中位数

时间:2019-02-28 16:28:19

标签: sas boxplot sgplot

我正在尝试通过使用SAS中的SGPLOT制作箱形图。我想将SGPLOT与VBOX语句一起使用,以在每个框的gragh上标记均值和中位数。

下面是我作为示例创建的数据集。有人可以帮我吗?

enter image description here

/* Set the graphics environment */                                                                                                     
goptions reset=all cback=white border htitle=12pt htext=10pt;                                                                           

/* Create a sample data set to plot */                                                                                                 
data one(drop=i);                                                                                                                       
   do i=1 to 10;                                                                                                                        
      do xvar=1 to 9 by 2;                                                                                                              
         yvar=ranuni(0)*100;                                                                                                            
         output;                                                                                                                        
      end;                                                                                                                              
   end;                                                                                                                                 
run;                                                                                                                                    

/* Sort the data by XVAR */                                                                                                            
proc sort data=one;                                                                                                                     
   by xvar;                                                                                                                             
run;                                                                                                                                    

/* Use the UNIVARIATE procedure to determine */                                                                                         
/* the mean and median values */                                                                                                       
proc univariate data=one noprint;                                                                                                       
   var yvar;                                                                                                                            
   by xvar;                                                                                                                             
   output mean=mean median=median out=stat;                                                                                             
run;                                                                                                                                    

/* Merge the mean and median values back */                                                                                             
/* into the original data set by XVAR    */                                                                                             
data all;                                                                                                                               
   merge one stat;                                                                                                                      
   by xvar;                                                                                                                             
run;

1 个答案:

答案 0 :(得分:2)

使用VBOX表示箱形图,使用SCATTER表示均值/中位数。

/*--Compute the Mean and Median by sex--*/
proc means data=sashelp.heart;
  class deathcause;
  var cholesterol;
  output out=heart(where=(_type_ > 0) keep=deathcause mean median  _type_)
    mean = mean
        median = median;
  run;

/*--Merge the data--*/
data heart2;
  keep deathcause mean median cholesterol;
  set sashelp.heart heart;
run; 
proc print data=heart2;run;

/*--Box plot with connect and group colors--*/
ods graphics / reset ANTIALIASMAX=5300 width=5in height=3in imagename='Box_Group_Multi_Connect';
title 'Cholesterol by Cause of Death';
proc sgplot data=heart2 noautolegend noborder;
  vbox cholesterol / category=deathcause group=deathcause;
  scatter x=deathcause y=mean / name='mean' legendlabel='Mean' markerattrs=(color=green);
  scatter x=deathcause y=median / name='median' legendlabel='Median' markerattrs=(color=red);
  keylegend "mean" "median" / linelength=32 location=inside across=1 position=topright;
  xaxis display=(nolabel);
run;

编辑:在SGPLOT和VBOX语句中,您还可以将中位数绘制为直线,将平均值绘制为箱形图上的一个点,而无需提前进行任何其他手动计算。自SAS 9.4 M5 +起可用。

ods graphics / reset ANTIALIASMAX=5300 width=5in height=3in imagename='Box_Group';
title 'Cholesterol by Cause of Death';
proc sgplot data=sashelp.heart noborder;
  vbox cholesterol / category=deathcause 
                    displaystats=(median mean) 
                    meanattrs=(color=red) 
                    medianattrs=(color=green);
  *xaxis display=(nolabel);
run;