[R 프로그래밍] 11-2. 데이터 시각화 - par

Author : tmlab / Date : 2016. 10. 4. 01:05 / Category : Lecture/R 프로그래밍

학습 목표

  • 전체 graphics 환경에 영향을 주는 par() 함수에 대해 배워보도록 하겠습니다.

목차

  1. layout()
  2. fig 인수
  3. new 인수
  4. bty 인수
  5. pty 인수
  6. type 인수
  7. pch 인수
  8. lty 인수
  9. xlab,ylab 인수
  10. xlim, ylim 인수
  11. col 인수
  12. cex 인수
  13. srt 인수
  14. tck 인수
  15. tcl 인수
  16. mar 인수
  17. oma 인수
  18. family, font 인수
  19. fg, bg 인수
  20. par의 default값으로 재적용 하는 방법

par( )함수

  • 그래픽 인수를 조회하거나 설정
    • 그래픽 인수는 그래픽 속성을 정의한 값으로, 화면의 분할 방법 및 마진 등의 설정 값을 말
  • 전체 그래픽에 영향을 줌
    • 단 plot에서 해당 인수를 바꾸게 되면, 그 설정에 맞게 됨
    • e.g., par로 점의 색을 red로 했는데, plot에서 blue로 주면 해당 plot의 점의 색깔은 파란색임

(1)layout( ) 함수

  • layout 함수
    • mfrow나 mfcol인수와 유사하지만 이 두 함수는 그림 영역을 가로와 세로를 격자처럼 대칭으로 분할 하는데 비해 layout()함수는 대칭이 아닌 모양으로도 분할이 가능함
(m <- matrix(c(1,1,2,3), ncol = 2, byrow = T))
##      [,1] [,2]
## [1,]    1    1
## [2,]    2    3
layout(mat = m)
plot(cars, main = "scatter plot of cars data", pch = 19, col = 4)
hist(cars$speed)
hist(cars$dist)

(2)fig인수

  • fig 인수
    • 그림 영역의 크기와 위치를 설정함
    • 인수 값은 c(x1,x2,y1,y2) 형식으로 지정
      • x1 - 그래픽 장치의 영역에서 그림 영역이 차지할 좌측 위치
      • x2 - 그래픽 장치의 영역에서 그림 영역이 차지할 우측 위치
      • y1 - 그래픽 장치의 영역에서 그림 영역이 차지할 하단 위치
      • y2 - 그래픽 장치의 영역에서 그림 영역이 차지할 상단 위치
op <- par(no.readonly = TRUE)
par(mfrow = c(2,2))
par(fig = c(0, 1, 0.5, 1))                                 #(1)
plot(cars, main = "scatter plot of cars data by fig")
par(fig = c(0, 0.5, 0, 0.5), new = T )                     #(2)
hist(cars$speed, main = "Histogra of cars$speed by fig")
par(fig = c(0.5, 1, 0, 0.5), new = T)                      #(3)
hist(cars$dist, main = "Histogra of cars$dist by fig")

par(op)

(3) new인수

  • new 인수
    • 고수준 그래픽 함수가 호출될 때 그림 영역의 내용을 초기화하는 여부를 결정하는 인수
    • default 값은 F이며, True로 할당할 경우 기존의 그림위에 새로운 plot을 덮어씌움
op <- par(no.readonly = TRUE)
par(mfrow = c(2,2))
plot(1:10, type = "l", main = "plot")
par(new = F)
plot(10:1, type = "s", main = "plot by new = F")
plot(1:10, type = "l")
par(new = T)
plot(10:1, type = "s", main = "plot by new = T")
x <- rnorm(10)
plot(x)
par(new = T)
hist(x)

par(op)

(4) bty인수

  • 플롯영역을 둘러싼 상자 모양 설정
  • 박스의 종류는 각각 “o”,“l”,“7”,“c”,“u”,“]”이 존재함
  • default는 “o”"
op <- par(no.readonly = T)
par(mfrow=c(2,3),bty="l")
#C모양(1,2,3 영역)
plot(0:6,0:6,bty="c",main="bty = \"c\" ")
# 출력하지 않음
plot(0:6,0:6,bty="n",main="bty = \"c\" ")
# O모양(1,2,3,4 영역)
plot(0:6,0:6,bty="o",main="bty = \"o\" ")
# 7모양(3,4 영역)
plot(0:6,0:6,bty="7",main="bty = \"7\" ")
# U모양(1,2,4 영역)
plot(0:6,0:6,bty="u",main="bty = \"u\" ")
# L모양(1,2 영역)
plot(0:6,0:6,bty="l",main="bty = \"l\" ")

par(op)

(5) pty인수

  • 플롯 영역의 형태를 지정
  • “s” - “square”
    • 플롯 영역을 x축과 y축의 비율이 동일하게 설정
  • “m” - “maximal”
    • 최대 크기로 플롯영역을 설정한다
op <- par(no.readonly = TRUE)
theta <- seq(-pi, pi, length = 30)
x <- cos(theta)
y <- sin(theta)
par(mfrow = c(1, 2), pty = "s", bty = "o")
plot(x, y, type = "l", main = "pty = \"s\"")
par(pty = "m")
plot(x,y, type = "l", main = "pty = \"m\"")

par(op)

(6) type 인수

  • 좌표영역에 데이터가 표현되는 형태 설정
par(mfrow=c(2,3),type="n")
## Warning in par(mfrow = c(2, 3), type = "n"): 그래픽 파라미터 "type"는 필요
## 하지 않습니다
plot(0:6,0:6,main = "default")
plot(0:6,0:6,type="b",main="type=\"b\"")
plot(0:6,0:6,type="c",main="type=\"c\"")
plot(0:6,0:6,type="o",main="type=\"o\"")
plot(0:6,0:6,type="s",main="type=\"s\"")
plot(0:6,0:6,type="S",main="type=\"S\"")

par(op)

(7) pch 인수

  • 점으로 표시될 문자 지정(기본값=1 속빈 동그라미)
  • 일반적으로 선호하는 값은 16이나 19: 속이 찬 동그라미

  • pch 인수

par(mfrow=c(2,3),pch=1)
plot(0:6,0:6,main = "default")
plot(0:6,0:6,pch=2,main="pch=\"2\"")
plot(0:6,0:6,pch=3,main="pch=\"3\"")
plot(0:6,0:6,pch=16,main="pch=\"16\"")
plot(0:6,0:6,pch=19,main="pch=\"19\"")
plot(0:6,0:6,pch=25,main="pch=\"25\"")

par(op)

(8) lty 인수

  • 선의 종류 지정(기본값=1 실선)

  • lty 인수

par(mfrow=c(2,3),lty=1)
plot(0:6,0:6,type="o",main = "default")
plot(0:6,0:6,type="o",lty=2,main="lty=\"2\"")
plot(0:6,0:6,type="o",lty=3,main="lty=\"3\"")
plot(0:6,0:6,type="o",lty=4,main="lty=\"4\"")
plot(0:6,0:6,type="o",lty=5,main="lty=\"5\"")
plot(0:6,0:6,type="o",lty=6,main="lty=\"6\"")

par(op)

(9) xlab, ylab 인수: x축, y축 이름 지정

(10) xlim, ylim 인수

  • x축, y축 범위 지정

  • xlim, ylim 인수

op <- par(no.readonly = TRUE)
x <- 0:4
set.seed(7)
 (y <- dbinom(x, size = 4, prob = 0.5))
## [1] 0.0625 0.2500 0.3750 0.2500 0.0625
par(oma = c(0, 0, 2, 0), mfrow = c(2, 2))
plot(x, y, type = "h", main = "default")
plot(x, y, type = "h", ylim = c(0, max(y)), main = "ylim = (0, max(y))")
plot(x, y, type = "h", ylim = c(0.1, 0.3), main = "ylim = c(0.1, 0.3)")
plot(x, y, type = "h", xlim = c(1, 3), main = "xlim = c(1, 3)") 
title(main = "binommial density", line = 0, outer = T)

par(op)

(11) col 인수

  • 색상 지정

(12) cex 인수

  • 문자나 점의 크기 지정

  • cex 인수

op <- par(no.readonly = TRUE)
par(mfrow = c(2, 2), oma = c(0, 0, 2, 0), cex = 1)
plot(0:6, 0:6, type = "n", main = "cex in text")             #(1-1)
text(1:3, 1:3, labels = LETTERS[1:3], cex = 1:3)             #(1-2)
plot(0:6, 0:6, type = "n", cex = 2, main = "cex in plot")   
text(1:3, 1:3, labels = LETTERS[1:3], cex = 1:3)             #(2)
plot(0:6, 0:6, type = "n", main = "cex in par")              
text(1:3, 1:3, labels = LETTERS[1:3], cex = 1:3)             #(3)
plot(0:6, 0:6, type = "n", main = "cex in par") 
text(1:3, 1:3, labels = c("가", "나", "다"), cex = 1:3)      #(4)
points(3:5, 1:3, pch = 1:3, cex = 1:3)
title(main = "cex", line = 0, outer = T)

par(op)

(13) srt 인수

  • 문자열을 회전하여 출력, text 함수에서 사용가능

  • srt 인수

op <- par(no.readonly = TRUE)
par(mar = c(2, 2, 2, 2))
plot(0:6, 0:6, type = "n", axes = F, xlab = "", ylab="")
text(3, 5, "srt =0", srt = 0, cex = 2)
text(1, 3, "srt = 90", srt = 90, cex = 2)
text(3, 1, "srt = 180", srt = 180, cex = 2)
text(5, 3, "srt = 270", srt = 270, cex = 2)
text(5, 5, "srt = -45", srt = -45, cex = 2)
text(1, 5, "srt = 45", srt = 45, cex = 2)
points(3, 3, pch = "A", srt = 45, cex = 2)
title("srt", srt = 45)
mtext(side = 2, "srt = 270", srt = 270, cex = 2)
axis(side = 1, srt = 45)

par(op)

(14) tck 인수

  • 좌표눈금선인 틱(ticks)의 길이 지정
    • 플롯영역의 크기를 기준으로 계산

(15) tcl 인수

  • 좌표눈금선인 틱(ticks)의 길이 지정
  • cex = 1일 때의 문자길이를 tcl = 1로 정하여 계산

(16) mar 인수

  • 플롯 영역의 마진 설정
  • 기본값은 c(5, 4, 4, 2) + 0.1
  • 아래, 왼쪽, 위쪽, 오른쪽 마진(순서대로)

  • mar 인수

op <- par(no.readonly = TRUE)
par(mfrow = c(2, 2))
par("mar")
## [1] 5.1 4.1 4.1 2.1
# Figure 1
par(mar = c(0, 0, 0, 0))                       #(1)
plot(0:4, 0:4)
title("mar = c(0, 0, 0, 0)")
#Figure 2
par(mar = c(2, 2, 2, 2))                       #(2)
plot(0:4, 0:4, main = "mar = c(2, 2, 2, 2)")
#Figure 3
par(mar = c(5, 5, 5, 5))                       #(3)
plot(0:4, 0:4, main = "mar = c(5, 5, 5, 5)")
#Figure 4
par(mar = c(1, 2, 3, 4))                       #(4)
plot(0:4, 0:4, main = "mar = c(1, 2, 3, 4)")

par(op)

(17) oma 인수

  • 바깥 마진의 크기 설정

(18) family, font

  • family 인수: 출력되는 문자의 폰트 종류 설정
  • font 인수: 출력되는 폰트의 체형을 설정
    • 1: 기본
    • 2: bold체
    • 3: italic체
    • 4: bold italic체
    • 5: symbol
  • font 인수
op <- par(no.readonly = TRUE)
par(mar = c(2, 2, 2, 2))
plot(1:10, type = "n", main = "par(font)", axes = FALSE, ylab = "", 
     xlab = "")
lab <- "Written with font parameter"
for (i in 1:10) { 
  par(font = i)
  text(5.5, 11 - i, labels = paste(lab, i), adj = 0.5, cex = 1.5)
}
box( )

par(op)

(19) fg,bg

  • fg 인수: 전경색 설정
  • bg 인수: 배경색 설정

  • fg, bg 인수

op <- par(no.readonly = TRUE)
 # 기본 값 조회
unlist(par("fg", "bg"))
##      fg      bg 
## "black" "white"
par(bg = "thistle", fg = "blue")
hist(rnorm(30), main = "big = \"thistle\", fg = \"blue\"" )

par(op)

(20) par의 default값으로 재적용 하는 방법

  • par(no.readonly=T)를 주시면 현재 환경의 par의 인수 값들이 저장이 됨
    • 따라서 par를 조작하기 전에 미리 특정 변수에 이 값들을 저장하고, 시각화가 종료 된 후에 다시 재적용 하면 default 셋팅으로 돌아가게 됩니다
    • 단 이미 조작된 상황에서 저장을 할 경우, 그 상태로 계속 돌아가게 됨
    • 해당 상황일 경우, rstudio plots 탭의 빗자루를 클릭하여 reset하면 됨

Save / Reset Par() – Graphical Parameters in R / RStudio

op <- par(no.readonly = T)
par(mfrow=c(2,2))
for(i in 1:3){
  plot(mtcars$wt,mtcars$mpg)
  Sys.sleep(0.5)
}
par(op)

plot(mtcars$wt,mtcars$mpg)


Archives

05-17 01:00

Contact Us

Address
경기도 수원시 영통구 원천동 산5번지 아주대학교 다산관 429호

E-mail
textminings@gmail.com

Phone
031-219-2910

Tags

Calendar

«   2024/05   »
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
Copyright © All Rights Reserved
Designed by CMSFactory.NET