pyll
pyll

Reputation: 1764

endpoint histogram SAS sgplot

I am trying to modify a histogram in SAS proc sgplot so that the bins are not graphically centered at the midpoint. Rather, I would like them to be from 0 to 5, 5 to 10, etc. The way this graph is currently, I get this weird "half bar" at 0--it's not the right thickness and it looks strange.

I'm not tied to using sgplot, I could use univariate or proc template if I can still get the desired overlaid histograms. Here is my data:

data have;
    input x y;
cards;
1 . 
5 .  
6 . 
20 . 
13 . 
4 . 
2 . 
2 . 
7 . 
4 . 
17 . 
33 . 
2 . 
. 2 
. 15 
. 4 
. 10 
. 35 
. 20 
. 6 
. 14
;
run;

proc sgplot data=have;
    histogram x /  fillattrs=graphdata1 name='s' legendlabel='x' 
                       transparency=0.5 binstart=0 binwidth=5; 
    histogram y / fillattrs=graphdata2 name='d' legendlabel='y' 
                       transparency=0.5  binstart=0 binwidth=5; 
    keylegend 's' 'd' / location=inside position=topright across=1;
    xaxis display=(nolabel) label='label' min=0 max=40;
 run;

Thanks.

Pyll

Upvotes: 1

Views: 3813

Answers (1)

Joe
Joe

Reputation: 63424

proc sgplot data=have;
    histogram x /  fillattrs=graphdata1 name='s' legendlabel='x' 
                       transparency=0.5 binstart=2.5 binwidth=5; 
    histogram y / fillattrs=graphdata2 name='d' legendlabel='y' 
                       transparency=0.5  binstart=2.5 binwidth=5; 
    keylegend 's' 'd' / location=inside position=topright across=1;
    xaxis display=(nolabel) label='label' min=0 max=40;
 run;

The BINSTART parameter is the midpoint for the first bin, not the low-endpoint. You want 2.5 to be the midpoint, so define it that way.

GTL does allow you to define the bars the way you want to, and it's possible that in a later release this option might be in SGPLOT. Note the xvalues=leftpoints, that is what controls how the bins are structured.

 proc template;
 define statgraph myhist;
    begingraph;
        layout overlay/cycleattrs=true x2axisopts=(labelFitPolicy=Split) xaxisopts=( display=( ticks tickvalues line ) 
                        type=linear linearopts=( viewmin=0 viewmax=40 ) );

            histogram x/binstart=0 binwidth=5 xvalues=leftpoints datatransparency=0.5 legendlabel='x'
                        name='x' fillattrs=graphdata1;
            histogram y/binstart=0 binwidth=5 xvalues=leftpoints datatransparency=0.5 legendlabel='y' 
                        name='y' fillattrs=graphdata2;
               DiscreteLegend "x" "y"/ Location=Inside across=1 halign=right valign=top;
        endlayout;
    endgraph;
end;
quit;

proc sgrender template=myhist data=have;
run;

Upvotes: 1

Related Questions