그래픽 디바이스에 그래프가 그려질 때 한 면에 하나 이상의 그래프를 그릴 수 있는데, 이런 경우 그래픽 디바이스의 영역을 나누는 레이아웃을 설정하는 함수
par() 함수에 mfrow, mfcol 옵션을 사용하는 경우, 그래프가 위에서 아래로 채워지는 반면, layout() 함수를 이용하면 그 순서를 임의로 정할 수 있음. 행과열의 크기도 조절 가능.
layout(matrix(3:1))
layout.show(3)
layout(matrix(c(2,1,0,3), ncol=2))
layout.show(3)
layout(matrix(c(2, 1, 2, 3), ncol = 2), heights = c(1.5, 1), respect = T)
hist(rnorm(100))
hist(rnorm(200))
hist(rnorm(300))
par(mfrow = c(2, 1))
set.seed(10)
samp <- rnorm(1000)
hist(samp, prob = T) # 히스토그램
lines(density(samp), col = 'red')
plot(mpg~wt, data = mtcars) # 산포도
abline(lm(mpg~wt, data = mtcars), col = 'red')
plot(mtcars$wt, mtcars$mpg, pch = (1:3)[as.factor(mtcars$cyl)]) # 산포도 그라기
legend("topright", legend = levels(as.factor(mtcars$cyl)), pch = 1:3) # 범례생성
title(main = "mpg vs. wt and cyl") # 제목 추가
library(ggplot2)
ggplot(mtcars, aes(wt, mpg, shape = as.factor(cyl))) +
geom_point() +
ggtitle("mpg vs. wt") +
scale_shape_discrete(name = "Number of \ncylinder")
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
facet_grid(cyl~gear)
- 위 그래프의 경우, mtcars 데이터셋의 wt 와 mpg 의 관계를 보여주는데, cyl 과 gear에 따라 어떻게 달라지는지 보여줌 -> 네 변수의 관계를 살필 수 있음.
그래프의 크기 조절은 그래픽 디바이스를 기준으로 정하는 방법과 출력물을 기준으로 정하는 방법이 있음. * 그래픽 디바이스를 기준으로 그래프의 크기를 조절하는 것은 인치 단위로 절대값을 정하는 것 * 출력물을 기존으로 그래프의 크기를 조절하는 것은 출력물에 따라 상대적인 값으로 정하는 것을 의미
| 방법 | 옵션 | 디폴트 | 사용법 |
|---|---|---|---|
| 그래픽 디바이스 크기를 조절 | fig.width fig.height | 7 | 인치 단위의 숫자 사용 |
| 출력물 기준으로 조절 | out.width out.height | NULL | 문자열로 준다. 인치 이외의 단위는 이 방법을 사용함 (예: 5cm, 300px) |
library(ggplot2)
data(iris)
ggplot(iris, aes(Sepal.Length, Sepal.Width, col=Species)) +
geom_point()
library(ggplot2)
data(iris)
ggplot(iris, aes(Sepal.Length, Sepal.Width, col=Species)) +
geom_point()
레이텍에서는 \textwidth 의 상대적인 값을 많이 사용함.
\textwidth 는 출력문서에서 문단의 폭에 해당됨.옵션값을 벡터로 줌 : out.width = c(‘200px’, ‘300px’, ‘400px’)
library(ggplot2)
data(iris)
ggplot(iris, aes(Sepal.Length, Sepal.Width, col=Species)) +
geom_point()
library(ggplot2)
data(iris)
ggplot(iris, aes(Sepal.Length, Sepal.Width, col=Species)) +
geom_point()
코드와 같이 출력할 경우 코드와 그래프 출력물의 상대적인 위치를 지정
set.seed(13)
a <- rnorm(1800)
plot(a)
hist(a, prob=T)
lines(density(a), col='red')
set.seed(13)
a <- rnorm(1800)
plot(a)
hist(a, prob=T)
lines(density(a), col='red')
set.seed(7)
library(ggplot2)
a <- rnorm(1000, mean=58, sd=4)
hist(a)
df <- data.frame(a)
p <- ggplot(df, aes(a)) +
geom_histogram(binwidth = 1, fill="white", color="black", aes(y=..density..))
p
p <- p +
geom_density(fill="red", alpha=0.1, col="red")
p
p <- p +
geom_vline(aes(xintercept=mean(a)), col = "red", linetype = "dashed")
p
par() 를 사용할 때는 나눠지는 영역의 크기가 모두 같음. 그래프마다 나눠지는 크기를 다르게 하고 싶다면 layout() 을 이용할 수 있음.
par(mfrow=c(3,1))
data(iris)
hist(iris$Petal.Width)
hist(iris$Petal.Width)
hist(iris$Petal.Width, prob=T)
lines(density(iris$Sepal.Width), col="red")
par(), layout() 함수를 이용하여 그래프를 배치하는 방법은 전통적 그래픽 시스템으로 만든 그래프 에서만 적용됨.
library(HSAUR2) # water 데이터셋 이용
## Loading required package: tools
library(grid)
library(ggplot2)
library(gridExtra)
p1 <- ggplot(water, aes(hardness, mortality)) +
stat_smooth(method = "lm")
p1 <- p1 + geom_point(aes(col=location)) +
scale_x_continuous(limits=c(0,150)) +
theme(legend.position=c(0.85, 0.8))
p2 <- ggplot(water, aes(hardness)) +
geom_histogram(binwidth = 20, fill = "darkgray") +
scale_x_continuous(limits = c(0, 150))
p3 <- grid.rect(gp = gpar(col = "white"))
p4 <- ggplot(water, aes(location, mortality)) +
geom_boxplot() +
theme(axis.title.y = element_blank())
grid.arrange(p2, p3, p1, p4, ncol = 2, widths = c(1.2, 1))
names(pdfFonts())
## [1] "serif" "sans" "mono"
## [4] "AvantGarde" "Bookman" "Courier"
## [7] "Helvetica" "Helvetica-Narrow" "NewCenturySchoolbook"
## [10] "Palatino" "Times" "URWGothic"
## [13] "URWBookman" "NimbusMon" "NimbusSan"
## [16] "URWHelvetica" "NimbusSanCond" "CenturySch"
## [19] "URWPalladio" "NimbusRom" "URWTimes"
## [22] "ArialMT" "Japan1" "Japan1HeiMin"
## [25] "Japan1GothicBBB" "Japan1Ryumin" "Korea1"
## [28] "Korea1deb" "CNS1" "GB1"
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
p <- p + ggtitle("한글제목")
p <- p + theme(text = element_text(family = "NanumGothic"))
p
extrafont 패키지는 컴퓨터에 있는 트루타입 폰트를 읽어와서 R 에서 사용할 수 있게 해둠.
library(extrafont)
## Registering fonts with R
#font_import()
boxplot(Sepal.Width~Species, data = iris)
title(main = "박스그래프", family = "NanumGothic")