'aes(fill=variable)'에 해당되는 글 1건

  1. 2015.09.05 R ggplot2 그래프 색깔 설정 (colour setting)

이번 포스팅에서는 R ggplot2에서 그래프 색깔 설정 (colour setting), 조절하는 방법에 대해서 알아보겠습니다.  이전의 각 그래프 종류별로 소개한 내용에도 색깔 지정에 대한 내용이 조금씩 포함되어 있기는 합니다만, 이번 포스팅에서는 색깔 설정에 대한 경우의 수를 종합적으로 포괄해서 정리를 해보았습니다.

 

그래프에서 색깔을 특정 변수의 변화와 align하게 되면 x축과 y축의 2차원에 색깔이라는 차원을 추가함으로써 3차원의 정보를 제공할 수 있습니다.  회사 업무 보고서에 엑셀로 그린 그래프를 많이 사용하고는 하는데요, x축과 y축의 2차원을 넘어서는, 특정 조건에 따라서 색깔이나 모양을 달리해야 하는 경우라면 엑셀로 그래프를 그리는 것이 거의 불가능하게 됩니다.  이때 R이 데이터 전처리/변환부터 해서 인쇄용으로 바로 사용해도 좋을 만큼의 고품질의 그래프를 그리는데 아주 특출난 역할을 할 수 있습니다.  R 그래프에서 색깔을 자유자재로 사용할 수 있다면 미적으로도 보기에 좋겠지요?!

 

 

이번 예제에서 사용할 데이터는 MASS 패키지에 내장되어 있는 Cars93 데이터 프레임의 차 무게(Weight)와 고속도로연비(MPG.highway), 차 가(Price), 실린더 개수(Cylinders)의 4개 변수를 사용하겠습니다.

 

> library(MASS)
> str(Cars93)
'data.frame':	93 obs. of  27 variables:
 $ Manufacturer      : Factor w/ 32 levels "Acura","Audi",..: 1 1 2 2 3 4 4 4 4 5 ...
 $ Model             : Factor w/ 93 levels "100","190E","240",..: 49 56 9 1 6 24 54 74 73 35 ...
 $ Type              : Factor w/ 6 levels "Compact","Large",..: 4 3 1 3 3 3 2 2 3 2 ...
 $ Min.Price         : num  12.9 29.2 25.9 30.8 23.7 14.2 19.9 22.6 26.3 33 ...
 $ Price             : num  15.9 33.9 29.1 37.7 30 15.7 20.8 23.7 26.3 34.7 ...
 $ Max.Price         : num  18.8 38.7 32.3 44.6 36.2 17.3 21.7 24.9 26.3 36.3 ...
 $ MPG.city          : int  25 18 20 19 22 22 19 16 19 16 ...
 $ MPG.highway       : int  31 25 26 26 30 31 28 25 27 25 ...
 $ AirBags           : Factor w/ 3 levels "Driver & Passenger",..: 3 1 2 1 2 2 2 2 2 2 ...
 $ DriveTrain        : Factor w/ 3 levels "4WD","Front",..: 2 2 2 2 3 2 2 3 2 2 ...
 $ Cylinders         : Factor w/ 6 levels "3","4","5","6",..: 2 4 4 4 2 2 4 4 4 5 ...
 $ EngineSize        : num  1.8 3.2 2.8 2.8 3.5 2.2 3.8 5.7 3.8 4.9 ...
 $ Horsepower        : int  140 200 172 172 208 110 170 180 170 200 ...
 $ RPM               : int  6300 5500 5500 5500 5700 5200 4800 4000 4800 4100 ...
 $ Rev.per.mile      : int  2890 2335 2280 2535 2545 2565 1570 1320 1690 1510 ...
 $ Man.trans.avail   : Factor w/ 2 levels "No","Yes": 2 2 2 2 2 1 1 1 1 1 ...
 $ Fuel.tank.capacity: num  13.2 18 16.9 21.1 21.1 16.4 18 23 18.8 18 ...
 $ Passengers        : int  5 5 5 6 4 6 6 6 5 6 ...
 $ Length            : int  177 195 180 193 186 189 200 216 198 206 ...
 $ Wheelbase         : int  102 115 102 106 109 105 111 116 108 114 ...
 $ Width             : int  68 71 67 70 69 69 74 78 73 73 ...
 $ Turn.circle       : int  37 38 37 37 39 41 42 45 41 43 ...
 $ Rear.seat.room    : num  26.5 30 28 31 27 28 30.5 30.5 26.5 35 ...
 $ Luggage.room      : int  11 15 14 17 13 16 17 21 14 18 ...
 $ Weight            : int  2705 3560 3375 3405 3640 2880 3470 4105 3495 3620 ...
 $ Origin            : Factor w/ 2 levels "USA","non-USA": 2 2 2 2 2 1 1 1 1 1 ...
 $ Make              : Factor w/ 93 levels "Acura Integra",..: 1 2 4 3 5 6 7 9 8 10 ... 

 

ggplot2 패키지는 별도의 설치 및 호출이 필요하므로 아래의 절차를 먼저 시행합니다.

 

> install.packages("ggplot2") # 설치

> library(ggplot2)  

 

 

색상 변화를 잘 살펴볼 수 있도록 속이 빈 동그라미 모양인 shape=21 번의 도형을 사용해서 size=6 으로 큼지막하게 키워서 예를 들어보겠습니다. 

 

 

(1) 색깔 설정을 별도로 하지 않은 경우 디폴트 테두리 검정색 (default colour)

 

> # default colour
> ggplot(Cars93, aes(x=Weight, y=MPG.highway)) +
+   geom_point(shape=21, size=6)

 

 

 

 

 

(2) 테두리 선 색깔 지정 (colour : line colour setting)

 

> # 테두리 선 색깔 지정 (colour : line colour setting)
> ggplot(Cars93, aes(x=Weight, y=MPG.highway)) +
+   geom_point(shape=21, size=6, colour="blue")

 

 

 

 

 

 

(3) 도형의 속 채우기 색깔 지정 (fill : innner colour fill-up)

 

> # 도형의 속 채우기 색깔 (fill : inner colour fill-up)
> ggplot(Cars93, aes(x=Weight, y=MPG.highway)) +
+   geom_point(shape=21, size=6, fill="blue")

 

 

 

 

 

 

(4) 연속형 변수의 숫자에 따른 색깔 조절 : aes(fill=continuous variable)

 

> # 연속형 변수의 숫자에 따른 색깔 조절
> # (fill : colour change aligned with continuous numeric variable)
> ggplot(Cars93, aes(x=Weight, y=MPG.highway, fill=Price)) +
+   geom_point(colour="grey", shape=21, size=6)

 

 

 

 

 

 

(5) 범주형 변수의 범주/요인(factor)에 따른 색깔 조절 : aes(fill=categorical variable)

 

> # 범주형 변수의 범주/요인(factor) 따른 색깔 조절 
> # (fill : colour change aligned with categorical variable)
> ggplot(Cars93, aes(x=Weight, y=MPG.highway, fill=Cylinders)) +
+   geom_point(colour="grey", shape=21, size=6)

 

 

 

 

 

###  아래의 palette 설정은 범주형 데이터 (categorical data)에만 해당됩니다.  

 

(6) Palette = Oranges 색 설정 (pallet colour setting) : scale_fill_brewer()

 

> # Palette = Oranges 색 설정 (palette colour setting) : scale_fill_brewer()
> ggplot(Cars93, aes(x=Weight, y=MPG.highway, fill=Cylinders)) +
+   geom_point(colour="grey", shape=21, size=6) +
+   scale_fill_brewer(palette="Oranges") # Oranges

 

 

 

 

 

 

(7) Palette = Reds 색 설정 (pallet colour setting) : scale_fill_brewer()

 

> # Palette = Reds 색 설정 (palette colour setting) : scale_fill_brewer()
> ggplot(Cars93, aes(x=Weight, y=MPG.highway, fill=Cylinders)) +
+   geom_point(colour="grey", shape=21, size=6) +
+   scale_fill_brewer(palette="Reds") # Reds

 

 

 

 

 

(8) Palette = Blues 색 설정 (pallet colour setting) : scale_fill_brewer()

 

> # Palette = Blues 색 설정 (palette colour setting) : scale_fill_brewer()
> ggplot(Cars93, aes(x=Weight, y=MPG.highway, fill=Cylinders)) +
+   geom_point(colour="grey", shape=21, size=6) +
+   scale_fill_brewer(palette="Blues") # Blues

 

 

 

 

많은 도움 되었기를 바랍니다.

 

이번 포스팅이 도움이 되었다면 아래의 '공감 ~♡' 단추를 꾸욱 눌러주세요.^^

 

 

728x90
반응형
Posted by Rfriend
,