SAS Tech & Tip

BookmarkSubscribeRSS Feed

[ SAS 활용 노하우 ] 데이터 시각화 part1

Started ‎06-21-2021 by
Modified ‎06-22-2021 by
Views 7,107

 

 

 

실습을 위한 데이터 설정

 

데이터 시각화를 위해 헬스장 회원의 정보를 이용해 데이터 셋을 만들어 보겠습니다.

 

data work.info;
input id name $10. gender $ age height weight PT price;
datalines;
1901 강지우 W 20 160 60 1 250
1902 김도윤 M 21 175 80 1 230
1903 현수아 W 20 157 55 0 120
2001 도서아 W 22 164 68 0 150 
2002 김준우 M 24 169 70 0 180
1904 오현주 W 21 161 56 1 240
1905 방지희 W 20 151 53 1 260
1906 송나라 W 20 169 55 1 250
2003 도경인 M 23 189 72 1 250
2005 마지희 W 24 169 64 0 100
;
run;
proc print data = work.info;
run;

 

1.png

 

 

 

1. 세로 막대그래프

 

막대그래프는 변수의 빈도수를 나타내는 그래프입니다.

 

title 'barchart';
proc sgplot data = work.info;
vbar age;
run;

2.png

 

 

나이의 빈도를 그래프로 나타내었을 때 20살이 많이 있는 것을 볼 수 있습니다.

 

 

2. 가로 막대그래프

 

title 'barchart';
proc sgplot data = work.info;
hbar age;
run;

 

가로 막대 그래프는 세로 막대 그래프 코드의 vbar에서 hbar로 바꾸어 주면 됩니다.

 

3.png

 

 

 

3. 누적 막대 그래프

 

title 'barchart';
proc sgplot data = work.info;
vbar age / group=gender;
run;

 

'group' : 변수를 기준으로 그룹 간 비교를 해줍니다.

남자와 여자를 색깔별로 나타내고 남자의 수치에 누적해서 보여줍니다.

4.png

 

 

 

4. 그룹별 막대그래프

groupdisplay : 그룹화된 표식을 클러스터로 지정을 해줍니다.

클러스터 옵션을 주었습니다. 남여로 나누어 각각의 나이에 맞는 빈도수 그래프를 나타내는 것을 볼 수 있습니다.

title 'barchart';
proc sgplot data = work.info;
vbar age / group=gender groupdisplay=cluster;
run;

 

5.png

 

 

 

-1) 평균 표준편차를 이용한 막대그래프

PT를 받는 사람과 받지 않는 사람의 가격의 평균, 표준편차를 이용하여 막드 그래프를 그려봅니다.

 

title 'barchart';
proc sgplot data = work.info;
vbar PT / response=price limitattrs=stddev
limits=upper stat=mean;
run; 

 

6.png

 

화면을 분할하여 두 개의 그래프를 그립니다.

 

title 'boxplot';
proc sgpanel data = work.info;
panelby gender;
vbar PT / response=price limitstat=stddev
limits = upper stat = mean;
run;

 

panelby에 오는 변수에 때라 panel의 개수가 정해집니다. 성별로 구분하고 PT별 price의 평균, 표준편차를 출력합니다.

 

 

7.png

 

 

5. 파이차트 그리기

 

파이차트는 범주형 데이터를 이용하여 그릴 수 있습니다.

범주형이란 숫자가 아닌 문자로 이루어진 값을 말합니다.

 

proc sgpie data = work.info;
pie gender;
run;

 

8.png

 

헬스장에 남자에 비해 여자가 많이 다니고 있는 것을 볼 수 있습니다.

 

 

6. 상자그림

가격별 상자그림을 그립니다.

 

proc sgplot data = work.info;
vbox weight;
run;

9.png

 

상자그림에서 가운데 파란줄은 중위수를 의미하고 마름모 모양은 평균을 의미합니다.

성별에 따른 가격을 상자 수염도로 나타내보려고 합니다.

 

 

 

proc sgplot data = work.info;
vbox price / category=gender;
run;

 

10.png

 

 

 

7. 히스토그램

 

 

proc sgplot data = work.info;
histogram price;
run;

 

11.png

 

 

 

-1) 변수의 정규 밀도 함수 그리기

 

proc sgplot data = work.info;
histogram weight;
density weight;
density weight / type = kernel ;
keylegend / location = inside position =toprightkernel;
run;

 

 

12.png

 

keylegend : 범례를 추가해줍니다.

location : 그림 안/밖 범례를 지정해줍니다.

position : 그림 안/밖 범례 위치를 지정해줍니다.

 

 

여러 범주에 따른 panel을 그리기

scale을 proportion, count, percent로 지정합니다.

 

proc sgpanel data = work.info;
where PT = 1;
panelby gender age / rows=2 columns=2;
histogram price / scale=proportion;
run;

 

13.png

 

 

-2) transparency 그래프의 불투명도를 조절하기

 

proc sgplot data = work.info;
histogram price;
histogram height / transparency=0.5;
run;
proc sgplot data = work.info;
scatter x=weight y=height / group=gender;
run;

 

14.png

 

 

Version history
Last update:
‎06-22-2021 04:36 AM
Updated by:
Contributors

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Article Labels
Article Tags