SAS SGPLOT VBOX:在Boxplot上显示平均值和中值

问题描述 投票:1回答:1

我正在尝试使用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;
sas boxplot sgplot
1个回答
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;
© www.soinside.com 2019 - 2024. All rights reserved.