我有一个单列的表,我希望根据表的列生成直方图。
22334074
ods graphics / reset width = 6.4in height = 4.8in imagemap;
proc sgplot …
只有这4个数据值,直方图如下所示:
绘图例程计算箱的年龄范围和箱中心的位置。计算是一些你可以控制的内部算法 histogram 声明选项 / binstart= binwidth= nbins= 。
histogram
/ binstart= binwidth= nbins=
当然,直方图条是针对箱的,并且高度被缩放到落入箱中的值的相对计数。 y轴将是实际计数或计数百分比。你有4个值落入3个箱子,所以其中一个箱子的计数为2(或50%= 2/4)。最高值栏比最低值栏短,因为高值低于低值。
当您有更多数据时会发生什么?
下面是一些代码,它们创建符合正态分布的250个值并对其进行直方图,还显示了累积频率针图。
data work.have; do personid = 1 to 250; do until (18 <= age <= 60); age = floor(18 + (32 + sqrt(62) * rannor(123))); end; output; end; run; proc freq noprint data=have; table age / out=freq outcum; * data for needle plot; run; proc sgplot data=have; title height=14pt "Default bins"; histogram age / showbins; density age; density age / type=Kernel; run; proc sgplot data=have; title height=14pt "binstart=20 binwidth=2"; histogram age / showbins binstart=20 binwidth=2; density age; density age / type=Kernel; run; proc sgplot data=freq; title height=10pt "cum_freq needle plot of data from Proc FREQ output"; needle x=age y=cum_freq; run;
更多示例代码显示的效果 nbins 和 xaxis
nbins
xaxis
ods graphics / reset width=500px height=250px imagemap; proc sgplot data=have; title height=12pt "binstart=0 nbins=25"; title2 height=12pt "xaxis min=0 max=100"; histogram age / showbins binstart=0 binwidth=2 nbins=50; density age; density age / type=Kernel; xaxis min=0 max=100; run; proc sgplot data=have; title height=12pt "binstart=0 nbins=10"; title2 height=12pt "xaxis min=-100 max=200"; histogram age / showbins binstart=0 binwidth=2 nbins=50; density age; density age / type=Kernel; xaxis min=-100 max=200; run;
对于在不同分类组中查看变量分布的情况,您可能需要升级到SGPANEL:
data work.have2; do year = 2017, 2018; do group = 'Team A', 'Team B', 'Team C'; do _n_ = 1 to 250; personId + 1; do until (18 <= age <= 95); age = floor(6 + (32 + sqrt(95) * rannor(123))); end; output; end; end; end; run; ods graphics / reset; title; proc sgpanel data=have2; panelby year group / layout=lattice; histogram age; xaxis run;