Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
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
Tags
more
Archives
Today
Total
관리 메뉴

자료 매번 검색하기 귀찮아서 만든 블로그

GGPLOT2 - textgrob 사용하기 (합쳐진 그림에 라벨 넣기) 본문

R

GGPLOT2 - textgrob 사용하기 (합쳐진 그림에 라벨 넣기)

쿠키아버님 2022. 1. 22. 15:52
library(gridExtra)
library(grid)
library(gridtext)

df<-data.frame(x1=runif(5),
               x2=runif(5),
               x3=runif(5),
               x4=runif(5),
               y=runif(5))
               
p1 <- ggplot(data = df, aes(x = x1, y = y))+geom_line()
p2 <- ggplot(data = df, aes(x = x2, y = y))+geom_line()
p3 <- ggplot(data = df, aes(x = x3, y = y))+geom_line()
p4 <- ggplot(data = df, aes(x = x4, y = y))+geom_line()

p = list(p1,p2,p3,p4) 
grid.arrange(grobs=p, ncol = 2, nrow = 2)

위와 같이 4개의 그림을 하나로 합친 그림에 대해, 공통 라벨을 붙이는 방법 : textgrob를 사용한다

 

yleft <- textGrob("Common Y label (left)", 
                  rot = 90, 
                  gp = gpar(fontsize = 20))

yright <- textGrob("Common Y label (right)",
                   rot = 270, 
                   gp = gpar(fontsize = 20))

bottom <- textGrob("Common Bottom label", 
                   gp = gpar(fontsize = 20))
                   
grid.arrange(grobs=p, ncol = 2, nrow = 2, left = yleft, right = yright, bottom = bottom)

 

 

참고 자료 : https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html