지난번 포스팅에서는 그래프에 추가적인 정보를 입력하는 낮은 수준의 그래프 함수(low level graphic functions) 중에서

(1) 제목, XY축 Label 추가하는 title()

(2) XY축의 형태를 변환하는 axis(side, ...)

(3) 직선 연결, 추가 : lines(x, y), abline(a, b), abline(h=y), abline(v=x)

(4) 점 추가 : points(x, y)

(5) 문자 추가 : text(x, y, labels, ...), mtext()

(6) 범례 추가 : legend(x, y, legend, ...)

에 대해서 알아보았습니다.

 


이번 포스팅에서는 낮은 수준의 그래프 함수 네번째로 (7) 다각형 추가 : polygon(x, y, ...) 에 대해서 소개하겠습니다.



[ 낮은 수준의 그래프 함수 (Low level graphic function) ]

 

 

 

다각형을 그리는 polygon() 함수의 일반적인 사용법은 다음과 같습니다.

 

polygon(x, y = NULL, density = NULL, angle = 45,
        border = NULL, col = NA, lty = par("lty"),
        ..., fillOddEven = FALSE) 

 

 

 구분

기능 설명 

 x, y

  다각형의 좌표의 벡터

 density

  다각형 안을 채우는 음영 선의 밀도 (default 는 NULL)

 angle

  음영있는 선의 각도 (수평선을 기준으로 해서 반시계방향)

 border

  다각형 테두리의 색깔

 col

  다각형을 채우는 색깔 (default 는 NA)

 lty

  par() 에서 사용하는 선 유형

  : 0=blank, 1=solid (default), 2=dashed, 3=dotted, 4=dotdash,

    5=longdash, 6=twodash

 ...

  추가로 그래프 모수 사용 가능

 fillOddEven

  다각형의 음영을 논리적으로 조절하는 모드 (default 는 FALSE)

 

 

 

두 개의 삼각형을 테두리의 색깔(border)과 선 유형(lty), 다각형을 채우는 색깔(col), 다각형을 채우는 선의 밀도(density)와 각도(angle), 색깔(col)을 달리해서 그려보겠습니다.

 

x와 y의 좌표값의 중간에 "NA" value가 들어갔음에 주의해서 보시기 바랍니다.

> ##-------------------------------------
> ## polygon : polygon(x, y, ...)
> ##-------------------------------------
> 
> # Multiple polygons from NA values
> 
> plot(c(1, 6), c(-3.5, 3.5), type = "n")
> x <- c(1, 2, 3, NA, 4, 4, 6)
> y <- c(1, -3, 2, NA, -3, 3, -3)
> polygon(x, y,
+         col = c("yellow", "blue"),
+         border = c("black", "red"),
+         lwd = 2, 
+         lty = c("dotted", "solid"))
> title("Multiple polygons from NA values")

 

 

 

 

 

 

아래의 다각형은 위와 R script가 거의 동일한데요, 단 한가지 차이점이라면 x, y의 좌표값에 "NA" value가 없다는 점입니다.  "NA" value가 없다보니 R은 중간에 다각형을 분리하지를 못하고, 그냥 하나의 색깔, 선 유형, 다각형 채우는 색을 계속 사용하고 있습니다.

 

> ## what if no NA value
> plot(c(1, 6), c(-3.5, 3.5), type = "n")
> x <- c(1, 2, 3, 4, 4, 6)
> y <- c(1, -3, 2, -3, 3, -3)
> polygon(x, y,
+         col = c("yellow", "blue"),
+         border = c("black", "red"),
+         lwd = 2, 
+         lty = c("dotted", "solid"))
> title("Multiple polygons without NA value")

 

 

 

 

 

 

이번에는 다각형 안의 음영을 선으로 채우는 방법을 소개하겠습니다. density 로 선의 밀도(숫자가 클 수록 촘촘해짐)를 지정하고, angle 로 선의 각도(수평선을 기준으로 반시계방향)를 지정하면 됩니다.

 

> # Line-shaded polygons
> plot(c(1, 6), c(-3.5, 3.5), type = "n")
> x <- c(1, 2, 3, NA, 4, 4, 6)
> y <- c(1, -3, 2, NA, -3, 3, -3)
> polygon(x, y,
+         col = c("yellow", "blue"),
+         border = c("black", "red"),
+         lwd = 2, 
+         lty = c("dotted", "solid"), 
+         density = c(10, 20), 
+         angle = c(45, -45))
> title("Multiple polygons with Line-shaded density")

 

 

 

 

 

 

아래 다각형은 x, y 좌표값이 위에서 든 예시와는 좀 다르지요?  두개의 좌표값들 간의 거리에 색깔을 채워넣은 형태의 그래프인데요, polygon() 함수로 이런 그래프도 그릴 수 있다는 예시로 보면 좋겠다 싶어서 www.math.cula.edu 사이트에서 참조하였습니다.

 

> ## Color-shaded polygon
> # exmaple source : http://www.math.ucla.edu/~anderson/rw1001/library/base/html/polygon.html
> n <- 100
> xx <- c(0:n, n:0)
> yy <- c(c(0,cumsum(rnorm(n))), rev(c(0,cumsum(rnorm(n)))))
> plot   (xx, yy, type="n", xlab="Time", ylab="Distance")
> polygon(xx, yy, col="gray", border = "red")
> title("Distance Between Brownian Motions")

 

 

 

 * R script source : http://www.math.ucla.edu/~anderson/rw1001/library/base/html/polygon.html

 

 

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

 

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

 

728x90
반응형
Posted by Rfriend
,

지난번 포스팅에서는 그래프에 추가적인 정보를 입력하는 낮은 수준의 그래프 함수(low level graphic functions) 중에서

(1) 제목, XY축 Label 추가하는 title()

(2) XY축의 형태를 변환하는 axis(side, ...)

(3) 직선 연결, 추가 : lines(x, y), abline(a, b), abline(h=y), abline(v=x)

(4) 점 추가 : points(x, y)

(5) 문자 추가 : text(x, y, labels, ...), mtext()

에 대해서 알아보았습니다.

 


이번 포스팅에서는 낮은 수준의 그래프 함수 네번째로 (6) 범례 추가 : legend(x, y, legend, ...) 에 대해서 소개하겠습니다.



[ 낮은 수준의 그래프 함수 (Low level graphic function) ]

 

 

 

범례를 추가하는 legend()함수의 일반적인 사용법은 아래와 같습니다.

 

legend(x, y = NULL, legend, fill = NULL, col = par("col"),
       border = "black", lty, lwd, pch,
       angle = 45, density = NULL, bty = "o", bg = par("bg"),
       box.lwd = par("lwd"), box.lty = par("lty"), box.col = par("fg"),
       pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = lwd,
       xjust = 0, yjust = 1, x.intersp = 1, y.intersp = 1,
       adj = c(0, 0.5), text.width = NULL, text.col = par("col"),
       text.font = NULL, merge = do.lines && has.pch, trace = FALSE,
       plot = TRUE, ncol = 1, horiz = FALSE, title = NULL,
       inset = 0, xpd, title.col = text.col, title.adj = 0.5,
       seg.len = 2) 

 

 

옵션이 굉장히 많지요?  ^^;  그래프에 관한한 R 가지고 못하는게 거의 없다는게 바로 이런 겁니다.  그런데 이걸 다 설명하자니 시간이 너무 오래걸릴것 같기도 하고 대부분은 아마 거의 사용할 일이 없을 것 같기도 합니다.  그래서 많이 사용하는 옵션 위주로 몇 개만 선별해서 어려움없이 사용할 수 있는 정도로만 설명하겠습니다.

 

 

 구분

옵션 기능 

 x, y

  범례를 추가할 위치를 설정하는 3가지 방법

  (1) x, y 좌표를 입력하면 범례 사각형의 왼쪽 상단이 그 지점에 위치함

  (2) locator(1)을 입력하면 커서를 가리키는 지점에 범례 생성

  (3) 위치를 나타내는 아래의 9개 단어 중 하나를 선택해서 입력

    : "bottomright", "bottom", "bottomleft", "left", "topleft",

      "top", "topright", "right" and "center"

 legend

  길이가 1보다 큰 문자(character) 혹은 범례를 담은 벡터(expression vector)

 col

  색깔 지정 벡터

 lty

  선 유형 지정 벡터

 lwd

  선 두께 지정 벡터

 pch

  기호 지정 벡터

...

  그래픽 모수 추가 설정 가능

 

 

 

MASS 패키지에 내장된 Cars93 데이터프레임의 차종(Type) 별로 차 무게(Weight)와 고속도로 연비 (MPG.highway) 변수를 사용해서 산포도를 그려보겠습니다. 그리고 차종(Type) 그룹에 대한 범례(legend)를 추가해보겠습니다.

 

 

(1) x, y 좌표를 직접 입력하여 범례 위치 설정

 

> # to use Cars93 dataframe
> library(MASS)
> 
> # scatter plot
> attach(Cars93)
> 
> # adding points with different characters by condition
> plot(Weight, MPG.highway, type = 'n') # blank plot
> 
> # Type = Compact
> points(Weight[Type == "Compact"], MPG.highway[Type == "Compact"], pch = 0)
> 
> # Type = Large
> points(Weight[Type == "Large"], MPG.highway[Type == "Large"], pch = 1)
> 
> # Type = Midsize
> points(Weight[Type == "Midsize"], MPG.highway[Type == "Midsize"], pch = 17, col = "yellow")
> 
> # Type = Small
> points(Weight[Type == "Small"], MPG.highway[Type == "Small"], pch = 3)
> 
> # Type = Sporty
> points(Weight[Type == "Sporty"], MPG.highway[Type == "Sporty"], pch = 9)
> 
> # Type = Van
> points(Weight[Type == "Van"], MPG.highway[Type == "Van"], pch = 15, col = "blue")
> 
> title("adding legend to the plot")
> 
> 
> # adding legend to topright side
> legend(x = 3500, y = 50, 
+        c("Compact", "Large", "Midsize", "Small", "Sporty", "Van"), 
+        col = c("black", "black", "yellow", "black", "black", "blue"), 
+        pch = c(0, 1, 17, 3, 9, 15)
+ )
>

 

 

> detach(Cars93)

 

 

 

 

(2) 위치를 나타내는 말을 직접 입력하여 범례 위치 설정

 

> library(MASS)
> # scatter plot
> attach(Cars93)
> # adding points with different characters by condition
> plot(Weight, MPG.highway, type = 'n') # blank plot
> # Type = Compact
> points(Weight[Type == "Compact"], MPG.highway[Type == "Compact"], pch = 0)
> 
> # Type = Large
> points(Weight[Type == "Large"], MPG.highway[Type == "Large"], pch = 1)
> 
> # Type = Midsize
> points(Weight[Type == "Midsize"], MPG.highway[Type == "Midsize"], pch = 17, col = "yellow")
> 
> # Type = Small
> points(Weight[Type == "Small"], MPG.highway[Type == "Small"], pch = 3)
> 
> # Type = Sporty
> points(Weight[Type == "Sporty"], MPG.highway[Type == "Sporty"], pch = 9)
> 
> # Type = Van
> points(Weight[Type == "Van"], MPG.highway[Type == "Van"], pch = 15, col = "blue")
> 
> title("adding points with different characters by Car Types")
> 
> 
> # adding legend to topright side
> legend("topright", 
+        c("Compact", "Large", "Midsize", "Small", "Sporty", "Van"), 
+        col = c("black", "black", "yellow", "black", "black", "blue"), 
+        pch = c(0, 1, 17, 3, 9, 15)
+        )
> 

 


> detach(Cars93)

 

 

 

다음번 포스팅에서는 다각형(polygon)을 추가하는 방법에 대해서 소개하겠습니다.

 

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

 

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

 

728x90
반응형
Posted by Rfriend
,

지난번 포스팅에서는 그래프에 추가적인 정보를 입력하는 낮은 수준의 그래프 함수(low level graphic functions) 중에서

(1) 제목, XY축 Label 추가하는 title()

(2) XY축의 형태를 변환하는 axis(side, ...)

(3) 직선 연결, 추가 : lines(x, y), abline(a, b), abline(h=y), abline(v=x)

(4) 점 추가 : points(x, y)

에 대해서 알아보았습니다.

 


이번 포스팅에서는 낮은 수준의 그래프 함수 네번째로 (5) 문자 추가 : text(x, y, labels, ...), mtext() 에 대해서 소개하겠습니다.



[ 낮은 수준의 그래프 함수 (Low level graphic function) ]

 

 

 

그래프에 문자를 추가할 때 text() 함수와 mtext() 함수를 사용합니다. 차이점은 text()가 그래프 내에 문자를 추가할 때 사용하는 반면, mtext()는 외부 마진 영역(1 하단, 2 좌측, 3 상단, 4 우측)에 문자를 추가할 때 사용한다는 것입니다.

 

 

text() 함수와 mtext() 함수의 일반적인 사용법은 아래와 같습니다.

 

함수 (function)

사용법 (usage)

 text()

  text(x, y, labels = , pos = , ... )

 mtext()

  mtext("text to place", side = , line = , adj, outer = , ... )

 

 

 함수

옵션 

기능 설명 

 text()

x, y

  문자를 추가할 위치의 x, y좌표.

  단, x, y 좌표 대신에 locator(1) 을 입력하면 커서로 지적하는

  곳에 문자를 추가함

 labels = " "

  추가할 문자

 pos =

  좌표를 기준으로 문자를 입력할 상대적인 위치

   : 1=below, 2=left, 3=above(default), 4=right

 ...

  폰트, 색깔, 크기 등의 그래프 모수 지정

 mtext()

 "text to place"

  추가할 문자

 side =

  문자를 추가할 위치

   : 1=bottom, 2=left, 3=top(default), 4=right

 line =

  문자와 그래프와의 마진 거리

  (to indicate the line in the margin starting with 0 and moving out)

 adj =

  adj=0 : 왼쪽/아래쪽 정렬,

  adj=1 : 위쪽/오른쪽 정렬,

  생략 : 중앙 정렬

  (adj=0 for left/bottom alignment or adj=1 for top/right alignment)

 outer =

  outer=TRUE : 외부마진에 문자 추가

  outer=FALSE : 내부마진에 문자 추가

 ...

  폰트, 색깔, 크기 등의 그래프 모수 지정

 

 

 

MASS 패키지에 내장되어있는 Cars93 데이터프레임의 차 무게 (Weight), 고속도로 연비 (MPG.highway) 변수를 활용해서 산점도를 그리고, 모델명(Model) 변수를 가지고 text를 추가해보겠습니다.

 

> ##-------------------------------------------
> ## adding text to the plot : text(), mtext()
> ##-------------------------------------------
> 
> ## adding text within a plot : text()
> library(MASS)
> attach(Cars93)
> 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 ...
> 
> plot(Weight, MPG.highway, main = "scatter plot of MPG.highway ~ Weight")
>

 

 
 
> text(x = Weight, y = MPG.highway, labels = Model, pos = 3, cex = 0.5)

 

 

 

 

 

text(x, y, ) 좌표 대신에 text(locator(1), ) 옵션을 사용하면 커서로 문자가 들어갈 위치를 콕 찍어서 지정할 수 있습니다.  reproducible research 관점에서 보면 추천할 만한 방법은 아닌데요, x, y 좌표를 정확히 모르거나, 한번만 간편하게 그래프 그려서 볼 목적이라면 큰 문제는 없겠습니다.

 

 

> # placing text at the point of cursor : locator(1)
> text(locator(1), labels = "Low Mileage Per Gallon")
 

 

> detach(Cars93)

 

 

 

 

다음으로 mtext() 를 사용해서 그래프 외부 마진 영역에 문자를 추가해보는 예제입니다. 

title() 함수로 제목을 추가하는 것과 유사한 측면이 있는데요, mtext()의 경우 여러 개의 그래프를 결합했을 때 외부마진에 그래프 전체를 아우리는 제목을 자유롭게 추가할 수 있다는 유용한 장점이 있습니다.

 

 

> ## places text in one of the four margins : mtext()
> 
> # Save default par values
> op <- par(no.readonly = TRUE)
> 
> # combining 2 graphs in 1 row
> par(mfrow = c(1,2), # 1 row, 2 windows
+     oma = c(2, 2, 4, 1)) # outer margin
> 
> 
> attach(Cars93)
> 
> plot(Weight, MPG.highway, main = "MPG.highway ~ Weight") # plot 1
> plot(Horsepower, MPG.highway, main = "MPG.highway ~ Horsepower") # plot 2
> 

 


> 
> mtext("MPG.highway by Weight, Horsepower", 
+       side = 3, # which margin to place text. 1=bottom, 2=left, 3=top, 4=right
+       line = 1, # to indicate the line in the margin starting with 0 and moving out
+       adj = 2, # adj=0 for left/bottom alignment or adj=1 for top/right alignment
+       cex = 2, # font size
+       outer = TRUE) # outer = TRUE : to place text at outer margin
>

 

 

> > detach(Cars93) > > # Reset par to the default values at startup > par(op)

 

 

다음번 포스팅에서는 범례(legend) 추가하는 방법에 대해서 알아보겠습니다.

 

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

 

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

 

728x90
반응형
Posted by Rfriend
,

지난번 포스팅에서는 그래프에 추가적인 정보를 입력하는 낮은 수준의 그래프 함수(low level graphic functions) 중에서 첫번째로 제목, XY축 Label 추가하는 title()에 대해서 알아보았습니다.


이번 포스팅에서는 낮은 수준의 그래프 함수 두번째로 (2) XY축 형태 변환 : axis(side, ...) 에 대해서 소개하겠습니다.



[ 낮은 수준의 그래프 함수 (Low level graphic function) ]





XY축 형태를 변경하는 axis() 함수의 기본 사용법은 아래와 같습니다.


axaxis(side, at = NULL, labels = TRUE, tick = TRUE, line = NA,
     pos = NA, outer = FALSE, font = NA, lty = "solid",
     lwd = 1, lwd.ticks = lwd, col = NULL, col.ticks = NULL,
     hadj = NA, padj = NA, ...)




위의 axis() 함수의 모수 중엣서 사용빈도가 높은 주요 모수에 대해서 기능 설명과 예를 들어보겠습니다.  


 arguments

description 

 side

그래프의 어느 쪽에 축을 그릴지 지정하는 정수

(an integer specifying which side of the plot the axis is to be drawn on)

    1 = 아래쪽 (below side)
    2 = 왼쪽 (left side)
    3 = 위쪽 (above side)
    4 = 오른쪽 (right side)

 at

축의 눈금이 그려질 곳의 위치를 지정하는 점들 

(the points at which tick-marks are to be drawn)

 labels

축 눈금에 라벨 데이터를 입력하는 숫자형 벡터, 혹은 문자형 벡터

(numerical or a character or expression vector of labels to be placed at the tick-points) 

 tick

축의 눈금과 축의 선을 그릴지를 설정하는 논리형 값

(a logical value specifying whether tickmarks and an axis line should be drawn) 

 pos

해당 축이 다른 축과 교차하는 좌표

(the coordinate at which the axis line is to be drawn) 

lty 

축과 눈금의 선의 유형

(line type for both the axis line and the tick marks) 

lwd

축의 눈금의 선의 두께

(line widths for the axis line and the tick marks) 

 col

축과 눈금 선의 색깔

(colors for the axis line and the tick marks respectively) 

 tck

 눈금의 방향과 길이 설정

(direction and length of tick marks)

    - 양수 : 그래프 안쪽으로 눈금 생성

    - 음수 : 그래프 바깥쪽으로 눈금 생성

 hadj

 수평 라벨에 대한 수정

(adjustment for all labels parallel (‘horizontal’) to the reading direction)

 padj

 수직 라벨에 대한 수정
(adjustment for each tick label perpendicular to the reading direction)




X축과 Y축을 변경하려면 먼저 "axes = FALSE" 옵션을 사용해서 X축과 Y축을 제거해야 합니다.  X축만 선별적으로 제거하려면 xaxt = "n", Y축만 선별적으로 제거하려면 yaxt = "n" 옵션을 사용하면 되겠습니다.  아래에 각각의 예를 들어보았습니다.


> ##---------------------------------------------
> ## low level graphic functions : axis(side, ...)
> ##---------------------------------------------
> 
> library(MASS) # to use Cars93 dataframe
> 
> # Save default par values
> op <- par(no.readonly = TRUE)
> 
> # Change par() function options
> par(mfrow=c(2, 2)) # make frame by 2 row, 2 columns 
>     
> 
> # plot with X and Y axis
> plot(MPG.highway ~ Weight, Cars93,
+      main = "plot with X and Y axis")
> 
> # deleting X and Y axes : axes = FALSE
> plot(MPG.highway ~ Weight, Cars93, axes = FALSE, 
+      main = "axes = FALSE") 
> 
> 
> # deleting X axis : xaxt = "n"
> plot(MPG.highway ~ Weight, Cars93, xaxt = "n", 
+      main = "xaxt = n") 
> 
> 
> # deleting Y axis : yaxt = "n"
> plot(MPG.highway ~ Weight, Cars93, yaxt = "n", 
+      main = "yaxt = n") 
> 
> 
> # Reset par to the default values at startup
> par(op)



 




X축과 Y축의 범위를 알아보기 위해 summary() 함수로 기술통계량을 알아보겠습니다.


> # summary statistics
> 
> summary(Cars93$Weight) # X axis
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1695    2620    3040    3073    3525    4105 
> 
> summary(Cars93$MPG.highway) # Y axis
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  20.00   26.00   28.00   29.09   31.00   50.00




X축으로 Weight (차의 무게)를, Y축으로는 MPG.highway (고속도로 연비)으로 하는 산점도를 그린다고 했을 때, X축은 1600~4200까지의 범위로 100씩 증가하는 눈금을, Y축은 18~52까지의 범위로 2씩 증가하는 눈금으로 그려보겠습니다.


> # X axis (1600 ~ 4200, by 100)
> # Y axis (18 ~ 52 range, by 2)
> plot(MPG.highway ~ Weight, Cars93, axes = FALSE, 
+      xlim = c(1600, 4200),
+      ylim = c(18, 52), 
+      main = "scatter plot of Weight and MPG.highway") 
> 
> x <- seq(1600, 4200, by = 100)
> y <- seq(18, 52, by = 2)
> 
> 
> axis(side = 1, # bottom side
+      at = x, 
+      labels = TRUE, 
+      pos = 18, # coordinate of X axis starting point
+      tck = 0.02) # tick marks at vertical direction with 0.02 length
> 
> axis(side = 2, # left side
+      at = y, 
+      labels = TRUE, 
+      pos = 1600, # coordinate of Y axis starting point
+      tck = -0.02) # tick marks at horizontal direction with 0.02 length






다음번 포스팅에서는 직선 연결, 직선 추가하는 낮은 수준은 그래프 함수 lines(), abline() 에 대해서 알아보도록 하겠습니다.


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

 

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

 

728x90
반응형
Posted by Rfriend
,