Author : tmlab / Date : 2016. 10. 4. 01:05 / Category : Lecture/R 프로그래밍
(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)
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)
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)
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)
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)
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)
일반적으로 선호하는 값은 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)
선의 종류 지정(기본값=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)
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)
문자나 점의 크기 지정
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)
문자열을 회전하여 출력, 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)
아래, 왼쪽, 위쪽, 오른쪽 마진(순서대로)
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)
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)
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)
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)