%let gpath='.'; %let dpi=200; ods html close; ods listing gpath=&gpath image_dpi=&dpi; /*--Simulate some X-Y data by group--*/ data scatter; drop i; Grp='A'; do i=1 to 1000; x=2*abs(rannor(2)); y=abs(rannor(2)); output; end; Grp='B'; do i=1 to 300; x=0.25+1.5*abs(rannor(2)); y=0.5+abs(rannor(2)); output; end; Grp='C'; do i=1 to 100; x=0.25+1.5*abs(rannor(2)); y=0.75+abs(rannor(2)); output; end; run; /*--Write Binned data by X for groups using SGPLOT Histogram to data set--*/ ods _all_ close; ods output sgplot=xa; ods graphics / reset width=5in height=5in imagename='X_Hist'; proc sgplot data=scatter(where=(x le 5)); by grp; histogram x / scale=count binstart=0 binwidth=0.25; run; /*--Clean up data set for X Bins and counts by group--*/ data xBins; set xa(where=(Bin_X_Scale_count_Binstart_0___Y ne .)); drop x; rename Bin_X_Scale_count_Binstart_0___Y=count Bin_X_Scale_count_Binstart_0___X=xBin; run; /*ods html;*/ /*proc print;run;*/ /*ods html close;*/ /*--Sort data by X bins--*/ proc sort data=xBins out=xBinsByBin; by xBin; run; /*proc print;run;*/ /*--Stack group counts values by X bins--*/ data HighLowX; drop count; retain Low High; set xBinsbyBin; by xBin; if first.xBin then Low=0; High=Low+Count; output; Low=High; run; /*proc print;run;*/ /*--Write Binned data by Y for groups using SGPLOT Histogram to data set--*/ ods _all_ close; ods output sgplot=ya; ods graphics / reset width=5in height=5in imagename='Y_Hist'; proc sgplot data=scatter(where=(y le 3.5)); by grp; histogram y / scale=count binstart=0 binwidth=0.25; run; /*proc print;run;*/ /*--Clean up data set for Y Bins and counts by group--*/ data yBins; set ya(where=(Bin_Y_Scale_count_Binstart_0___Y ne .)); drop y; rename Bin_Y_Scale_count_Binstart_0___Y=count Bin_Y_Scale_count_Binstart_0___X=yBin; run; /*proc print;run;*/ /*--Sort data by Y bins--*/ proc sort data=yBins out=yBinsByBin; by yBin; run; /*proc print;run;*/ /*--Stack group counts values by Y bins--*/ data HighLowY; drop count; retain Low High; set yBinsbyBin; by yBin; if first.yBin then Low=0; High=Low+Count; output; Low=High; run; /*proc print;run;*/ /*--Merge original X-Y data with X Bin and Y Bin data--*/ data merged; set scatter HighLowX HighLowY; run; /*proc print;run;*/ /*--Draw each graph individually--*/ ods listing gpath=&gpath image_dpi=&dpi style=htmlblue; ods graphics / reset width=3in height=3in imagename='Scatter'; proc sgplot data=merged ; scatter x=x y=y / group=grp markerattrs=(symbol=circlefilled size=5); run; ods graphics / reset width=3in height=1.5in imagename='HighLow_X'; proc sgplot data=merged noautolegend; highlow x=xBin low=low high=high / group=grp type=bar lineattrs=(pattern=solid); xaxis values=(0 1 2 3 4 5); yaxis offsetmin=0 display=(nolabel); run; ods graphics / reset width=1.5in height=3in imagename='HighLow_Y'; proc sgplot data=merged noautolegend; highlow y=yBin low=low high=high / group=grp type=bar lineattrs=(pattern=solid); yaxis values=(0 1 2 3); xaxis offsetmin=0 display=(nolabel); run; /*--Template for 2x2 Scatter and stacked Histogram layout--*/ proc template; define statgraph Scatter_Layout; begingraph; entrytitle 'Distribution by Group'; /*--Outermost Lattice Container--*/ layout lattice / rows=2 columns=2 rowweights=(0.3 0.7) columnweights=(0.7 0.3) columndatarange=union rowdatarange=union rowgutter=5 columngutter=5; /*--Common Row axes--*/ rowaxes; rowaxis / offsetmin=0 display=(ticks tickvalues) griddisplay=on gridattrs=(color=cxf2f2f2); rowaxis / label='Mean of Full Rho' griddisplay=on gridattrs=(color=cxf2f2f2) linearopts=(tickvaluesequence=(start=0 increment=0.5 end=3.5)); endrowaxes; /*--Common Column axes--*/ columnaxes; columnaxis / label='Ratio of Full Rho' griddisplay=on gridattrs=(color=cxf2f2f2); columnaxis / offsetmin=0 display=(ticks tickvalues) griddisplay=on gridattrs=(color=cxf2f2f2); endcolumnaxes; /*--Upper Left cell with Stacked X Bins counts by group--*/ layout overlay; highlowplot x=xBin low=low high=high / group=grp type=bar lineattrs=(pattern=solid); endlayout; /*--Upper Right cell with Legend--*/ layout overlay; discretelegend 'a'; endlayout; /*--Lower Left cell with SX-Y Scatter Plot--*/ layout overlay; scatterplot x=x y=y / group=grp markerattrs=(symbol=circlefilled size=5) name='a'; endlayout; /*--Lower Right cell with Stacked Y Bins counts by group--*/ layout overlay; highlowplot y=yBin low=low high=high / group=grp type=bar lineattrs=(pattern=solid); endlayout; endlayout; endgraph; end; run; /*--Draw the Graph--*/ ods graphics / reset width=5in height=5in noscale imagename='Scatter_Layout'; proc sgrender data=merged(where=(x le 5 and y le 3.5)) template=Scatter_Layout; run;