'scale_x_continuous(breaks=c())'에 해당되는 글 1건

  1. 2015.09.04 R ggplot2 x축, y축 설정 : coord_fixed(), scale_x_continuous(), scale_y_continuous() 4

ggplot2 그래프를 그렸을 때 x축이나 y축의 디폴트 값을 사용해도 무리가 없는 경우가 많기는 합니다만, 

 

분석가가 x축이나 y축을 좀더 사용하는 목적에 맞게 설정을 조정하고 싶을 때가 있습니다. 

 

이때 사용할 수 있는 ggplot2의 함수 3가지를 알아보도록 하겠습니다. 

 

(1) 1:1 의 비율로 x축과 y축 설정

 : coord_fixed()

 

(2) 일정한 간격으로 x축, y축 설정

 : scale_x_continuous(breaks=seq())

 

(3) 분석가 마음대로 x축, y축 설정

 : scale_x_continuous(breaks=c())

 

 

예제로 사용할 데이터는 MASS 패키지에 내장되어 있는 Cars93 데이터 프레임의 도시연비(MPG.city), 고속도로연비(MPG.highway)가 되겠습니다.  scale이 연비로서 서로 같은 변수를 선택하였으며, 산점도를 그려보면서 x축, y축 설정을 바꿔보도록 하겠습니다.

 

> 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는 신규 설치 및 호출이 필요한 패키지이므로 아래와 같은 사전 절차가 필요합니다.

 

> # ggplot2 설치 및 호출

> install.packages("ggplot2") > library(ggplot2) 

 

순서대로 하나씩 살펴보겠습니다.  함수 옵션을 바꿔줌에 따라서 x축, y축 결과가 어떻게 바뀌는지 살펴보시고, 필요로 하는 함수 옵션을 선택해서 사용하시면 되겠습니다.

 

(0) Default setting

 

> # default setting of x and y axis
> ggplot(Cars93, aes(x=MPG.city, y=MPG.highway)) +
+   geom_point(shape=21, colour="black", size=3) + 
+   ggtitle("default setting of x and y axis") 
 

 

 

 

 

(1) 1:1 의 비율로 x축과 y축 설정 : coord_fixed()

 

> # 1:1 proportion of x and y axis : coord_fixed()
> ggplot(Cars93, aes(x=MPG.city, y=MPG.highway)) +
+   geom_point(shape=21, colour="black", size=3) + 
+   coord_fixed() +
+   ggtitle("1:1 proportion of x and y axis : coord_fixed()")

 

 

 

 

 

 

(2) 일정한 간격으로 x축과 y축 설정 : scale_x_continuous(breaks=seq())

 

> # manual setting with fixed interval of x and y axis : scale_x_continuous(breaks=seq())
> ggplot(Cars93, aes(x=MPG.city, y=MPG.highway)) +
+   geom_point(shape=21, colour="black", size=3) + 
+   coord_fixed() +
+   scale_x_continuous(breaks=seq(0, 80, 5)) + 
+   scale_y_continuous(breaks=seq(0, 80, 5)) + 
+   ggtitle("manual setting with fixed interval of x and y axis : scale_x_continuous(breaks=seq())")

 

 

 

 

 

 

 

(3) 분석가 마음대로 x축과 y축 설정 : scale_x_continuous(breaks=c())

 

> # manual setting of x and y axis : scale_x_continuous(breaks=c())
> ggplot(Cars93, aes(x=MPG.city, y=MPG.highway)) +
+   geom_point(shape=21, colour="black", size=3) + 
+   coord_fixed() +
+   scale_x_continuous(breaks=c(10, 15, 20, 25, 30, 40)) + 
+   scale_y_continuous(breaks=c(20, 25, 30, 40, 50)) + 
+   ggtitle("manual setting of x and y axis : scale_x_continuous(breaks=c())")

 

 

 

 

함수 옵션을 바꿔줌에 따라서 x축, y축 결과가 어떻게 바뀌는지 살펴보시고, 필요로 하는 함수 옵션을 선택해서 사용하시면 되겠습니다.

 

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

 

 

728x90
반응형
Posted by Rfriend
,