반응형

/*********************************************************************************************************
-- Title : [R3.3] Visualization - ggplot2 패키지 활용 및 차트 저장
-- Reference : hrd-net
-- Key word : R 시각화 차트 패키지 ggplot ggplot2 diamonds mtcars mpg qplot ggsave 차트 저장

*********************************************************************************************************/

-- Chart

-- R

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# ********************************************
# -- ggplot2 패키지
# ********************************************
install.packages("ggplot2")                                     # 패키지 설치
library(ggplot2)
 
# -- ggplot2 패키지 제공 데이터 셋
data(diamonds)                                                  # 데이터 셋 가져오기
data(mtcars)
data(mpg) 
 
str(mpg)                                                        # map 데이터 셋 구조 보기
head(mpg)                                                       # map 데이터 셋 내용 보기 
summary(mpg)                                                    # 요약 통계량
table(mpg$drv)                                                  # 구동방식 빈도수 
 
 
# ********************************************
# -- qplot() 함수
#    1개 변수 대상 기본 - 속이 꼭찬 막대 모양의 세로막대 그래프 
# ********************************************
qplot(hwy, data=mpg)                                            # 세로막대 그래프
 
# -- fill 옵션 : hwy 변수를 대상으로 drv변수에 색 채우기(누적 막대그래프) 
qplot(hwy, data=mpg, fill=drv)                                  # fill 옵션 적용
 
# binwidth 옵션 : 막대 폭 지정 옵션
qplot(hwy, data=mpg, fill=drv, binwidth=2)                      # binwidth 옵션 적용 
 
# facets 옵션 : drv변수 값으로 칼럼단위와 행 단위로 패널 생성
qplot(hwy, data=mpg, fill=drv, facets=.~ drv, binwidth=2)       # 열 단위 패널 생성
qplot(hwy, data=mpg, fill=drv, facets=drv~., binwidth=2)        # 행 단위 패널 생성
 
# -- 2변수 대상 기본 - 속이 꽉찬 점 모양과 점의 크기는 1를 갖는 산점도 그래프
qplot(displ, hwy, data=mpg)                                     # mpg 데이터셋의 displ과 hwy변수 이용
 
# -- displ, hwy 변수 대상으로 drv변수값으로 색상 적용 산점도 그래프
qplot(displ, hwy, data=mpg, color=drv)
 
# -- 색상, 크기, 모양 적용
### ggplot2 패키지 제공 데이터 셋
head(mtcars)
str(mtcars)                                                                         # ggplot2에서 제공하는 데이터 셋
qplot(wt, mpg, data=mtcars, color=factor(carb))                                     # 색상 적용
qplot(wt, mpg, data=mtcars, size=qsec, color=factor(carb))                          # 크기 적용
qplot(wt, mpg, data=mtcars, size=qsec, color=factor(carb), shape=factor(cyl))       #모양 적용 
mtcars$qsec
 
 
# --------------------------------------------
# -- geom 옵션 
# --------------------------------------------
# -- ggplot2 패키지 제공 데이터 셋
head(diamonds) 
 
# -- geom="bar" -> clarity변수 대상 cut변수로 색 채우기
qplot(clarity, data=diamonds, fill=cut, geom="bar")             # 레이아웃에 색 채우기
qplot(clarity, data=diamonds, colour=cut, geom="bar")           # 테두리 색 적용
 
# -- geom="point"
qplot(wt, mpg, data=mtcars, size=qsec)                          # geom="point" 기본
qplot(wt, mpg, data=mtcars, size=qsec, geom="point")
 
# -- cyl 변수의 요인으로 point 크기 적용, carb 변수의 요인으로 포인트 색 적용
qplot(wt, mpg, data=mtcars, size=factor(cyl), color=factor(carb), geom="point")
 
# -- qsec변수로 포인트 크기 적용, cyl 변수의 요인으로 point 모양 적용
qplot(wt, mpg, data=mtcars, size=qsec, color=factor(carb), shape=factor(cyl), geom="point")
 
# -- geom="smooth"
qplot(wt, mpg, data=mtcars, geom=c("point""smooth"))
qplot(wt, mpg, data=mtcars, color=factor(cyl), geom=c("point""smooth")) # cyl변수 요인으로 색상 적용
 
# -- geom="line"
qplot(mpg, wt, data=mtcars, color=factor(cyl), geom="line")
qplot(mpg, wt, data=mtcars, color=factor(cyl), geom="point"+ geom_line()
 
# -- geom="freqpoly"
qplot(clarity, data=diamonds, geom="freqpoly", group=cut, colour=cut)
 
 
# --------------------------------------------
# -- position 옵션 
#    다양한 bar 차트 유형("identity",stacked, dodged, identity)
# --------------------------------------------
 
# -- 채우기-가장 큰 값을 기준으로 채우기형 막대그래프
qplot(clarity, data=diamonds, geom="bar", fill=cut, position="identity")
 
# -- 채우기-가장 적은 값을 기준으로 채우기형 막대그래프 
qplot(clarity, data=diamonds, geom="bar", fill=cut, position="fill")
 
# -- 스택형태-누적형-기본형
qplot(clarity, data=diamonds, geom="bar", fill=cut, position="stack")
 
# -- 다지-살짝 비키다
qplot(clarity, data=diamonds, geom="bar", fill=cut, position="dodge")
 
 
# ********************************************
# -- ggplot()함수
# ********************************************
 
# -- aes(x,y,color) 옵션 
#    aes(x,y,color) 속성 = aesthetics : 미학
= ggplot(diamonds, aes(carat, price, color=cut))
+ geom_point()                                                # point 추가 
 
 
# -- geom_line() 레이어 추가 
= ggplot(mtcars, aes(mpg,wt,color=factor(cyl)))
+ geom_line()                                                 # line 추가
 
# -- geom_point()함수  레이어 추가
= ggplot(mtcars, aes(mpg,wt,color=factor(cyl)))
+ geom_point()                                                # point 추가
 
# -- geom_step() 레이어 추가
= ggplot(mtcars, aes(mpg,wt,color=factor(cyl)))
+ geom_step()                                                 # step 추가
 
# -- geom_bar() 레이어 추가
= ggplot(diamonds, aes(clarity))
+ geom_bar(aes(fill=cut), position="fill")                    # bar 추가
 
 
# --------------------------------------------
# -- ggsave()함수 : save image of plot on disk 
# --------------------------------------------
= ggplot(diamonds, aes(carat, price, color=cut))
+ geom_point()                                                # point 추가
 
ggsave(file="C:\\RProject\\Rwork\\output\\diamond_price.pdf")   # 가장 최근 그래프 저장
ggsave(file="C:\\RProject\\Rwork\\output\\diamond_price.jpg", dpi=72)
 
# -- 변수에 저장된 그래프 저장 
= ggplot(diamonds, aes(clarity))
= p + geom_bar(aes(fill=cut), position="fill")                # bar 추가
ggsave(file="C:\\RProject\\Rwork\\output\\bar.png", plot=p, width=10, height=5)
 
 

cs

반응형

+ Recent posts