본문 바로가기
SAS

[SAS 프로그래밍 실습] Histogram Overlay

by 웰러맨 2022. 1. 17.
반응형

Histogram Overlay

 

1. Introduction

안녕하세요. 박세훈입니다. 이번엔 비교적 가벼우면서 쓸 만한 히스토그램 다루기 내용을 가져왔습니다. 데이터는 Python 예제로도 많이 사용되는 Iris 데이터 입니다.

 

2. SGPANEL 사용

<code1>

proc univariate data=sashelp.iris;

class Species;

var SepalLength; /*기술 통계량 계산 */

histogram SepalLength / nrows=3 odstitle="PROC UNIVARIATE with CLASS statement";

ods select histogram; /* 히스토그램 표시 */

run;

Iris(붓꽃)데이터를 사용했구요, var로는 꽃받침 길이로 선택했습니다. 붓꽃에는 크게 3가지 종류가 있고, species가 그 해당 변수입니다. 3가지 히스토그램이 출력됩니다.

각 붓꽃 종류에 따라 그래프가 그려지고, wide 형태로 출력됩니다. 붓꽃 종류마다 꽃받침 길이가 달라 확연히 차이나는 그래프로 표현됩니다.

<code2>

title "PROC SGPANEL with PANELBY statement";

proc sgpanel data=sashelp.iris;

panelby Species / rows=3 layout=rowlattice;

histogram SepalLength;

run;

위와 같은 상황에서, 레이아웃을 컨트롤할 수 있는 SGPANEL을 사용하여 그래프를 작성할 수 있습니다. GTL을 사용하면 이보다 더 복잡한 Panel display를 작성할 수 있습니다. GTL에 대한 내용은 추후에 포스팅 하겠습니다.

 

3. 히스토그램 오버레이

위와 같은 3가지 이상의 그룹에서 SGPANEL을 사용할 수 있습니다. 이번에 해볼 것은 여러그룹으로 히스토그램을 작성할 경우 Transparency 옵션을 사용하여 히스토그램을 겹치게 제작하는 것입니다.

 

3.1 두 그룹 오버레이

<code3>

proc sgplot data=sashelp.iris;

where Species in ("Setosa", "Versicolor"); /* 그룹 선정 */

histogram SepalLength / group=Species transparency=0.5;/*투명도0.5 설정 */

density SepalLength / type=kernel group=Species; /* overlay 밀도 추정*/

run;

위와 같이 두 그룹이 겹쳐서 출력되는데, transparency를 사용하면 투명도를 조정할 수 있습니다.

 

 

3.2 각 다른 변수 오버레이

서로 다른 그룹 뿐아니라 변수끼리의 분포도 비교할 수 있습니다. 이 경우 Class 를 따로 지정하지 않습니다.

<code3>

proc sgplot data=Sashelp.Iris;

histogram PetalLength / binwidth=5 transparency=0.5

name="petal" legendlabel="Petal Width";

histogram SepalLength / binwidth=5 transparency=0.5

name="sepal" legendlabel="Sepal Width";

density PetalLength / type=kernel lineattrs=GraphData1; /* 옵션 */

density SepalLength / type=kernel lineattrs=GraphData2; /* 옵션 */

xaxis label="Length (mm)" min=0;

keylegend "petal" "sepal" / across=1 position=TopRight location=Inside;

run;

 

4. Conclusion

이번엔 비교적 간단하지만, 유용하게 시각화를 해보는 시간을 가졌습니다. 다양한 시각화 기법을 알고 있다면 데이터 리포트나 커뮤니케이션에 많은 도움이 될 것 같습니다.

반응형

댓글