Butterfly Plot
1. Introduction
안녕하세요. 이번 시간에는 시각화 기법에 대해 실습해보겠습니다. 지난 시간에 배웠던 Funnel Plot처럼, 특정 데이터에 대해 더 좋은 시각화 기능을 보여주는 여러 함수들이 있습니다. 오늘은 Butterfly Plot에 대해 배워보겠습니다.
2. Data
Butterfly Plot은 두 집단의 분포를 비교할 때 흔하게 사용되는 시각화 기법입니다. 대표적인 대조군인 성별을 비교해 보겠습니다. 데이터는 SAS 내장 데이터인 Sashelp.Heart를 사용하겠습니다.
3. 모델링
3.1 EDA
<code1>
proc univariate data=Sashelp.Heart;
class Sex;
var Cholesterol;
histogram cholesterol / nrows=2 outhist=OutHist
/* 분포 범위 (80 부터 560, 간격 40 ) */
odstitle="Cholesterol by Gender";
ods select histogram;
run;
콜레스테롤에 대한 성별 그룹 별 분포입니다. 기본적인 시각화 기법으로 분포를 확인했습니다.
3.2 Convert
기존의 OutHist 형식은 “Long Form”입니다. Butterfly Plot으로 변형하기 위해 데이터를 “ Wide Form” 형태로 바꿔줍니다.
<code2>
/* Long Form을 Wide Form으로 변형하기 */
data Butterfly;
keep Cholesterol Males Females;
label Males= Females= Cholesterol=; /* 라벨 제거*/
merge OutHist(where=(sex="Female") rename=(_COUNT_=Females _MIDPT_=Cholesterol))
OutHist(where=(sex="Male") rename=(_COUNT_=Males _MIDPT_=Cholesterol));
by Cholesterol;
Males = -Males; /* Count 표현을 위해 -Count를 트릭으로 지정하기*/
run;
실행 결과:
Males 데이터가 음수로 표현된 것을 볼 수 있습니다. 이를 통해 한 X축에 두 성별을 동시에 표현할 수 있습니다.
<code3>
/* 이후, 절대값을 표현할 Format을 지정합니다.
Format 지정이 없으면 음수가 그대로 출력됩니다. */
proc format;
picture positive low-<0="000,000"
0<-high="000,000";
run;
<code4>
proc sgplot data=Butterfly;
format Males Females postive.;
hbar Cholesterol / response=Males legendlabel="Males";
hbar Cholesterol / response=Females legendlabel="Females";
xaxis label="Count" grid
min=-520 max=520 values=(-500 to 500 by 100) valueshint;
yaxis label="Cholesterol" discreteorder=data;
run;
데이터 출력을 위한 서식을 지정합니다.
Cholesterol 수치를 그룹별로 확인이 쉬운 Butterfly Plot을 생성하였습니다.
4. Conclusion
비교적 간단한 시각화 기법이지만, 의학과 같은 대조군 실험에서 유용하게 사용될 수 있습니다. 고급 분석 기법은 아니지만 다양한 스킬들을 숙지하여 가독성이 좋은 리포트를 만드는 것도 분석가의 역량이 아닐까 생각합니다.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.