2016-08-05 1 views
3

짧은 버전 : 방법 axis.text와 axix.title 사이의 마진의 값을 얻는 방법?axis.text 및 axis.title 사이에 여백 값을 취득

긴 버전 : 나는 세 ggplot 그래픽을 병합하려합니다. 척도는 일관성이 있어야하므로 rbind (ggplotGrob 사용)를 사용하여 처음 두 그래픽을 정렬합니다. 그러나 마지막에는 패싯이 있으므로 세 가지 모두에 대해이 솔루션을 사용할 수 없습니다.

내 아이디어는 세 번째 그래픽에서 axis.title과 axis.text 사이의 공백을 수동으로 설정합니다 (이 작업은 element_text와 함께 여백을 사용하여 수행 할 수 있습니다). 그러나 이렇게하려면 처음 두 그래픽 중 하나에서이 여백의 값을 얻어야합니다. 이 정보는 ggplotGrob에서 사용할 수 있다고 믿지만 grob 구조에서이 값을 검색하는 경로를 알지 못합니다.

가짜 코드 예 :

library(ggplot2) 
library(gridExtra) 

a <- ggplotGrob(ggplot(iris[iris$Species != "setosa",], aes(x=Sepal.Length, y=Petal.Width*100000, color=Species)) + geom_line() + theme(legend.position="none")) 

b <- ggplotGrob(ggplot(iris[iris$Species != "setosa",], aes(x=Sepal.Length, y=Petal.Length, color=Species)) + geom_line() + theme(legend.position="none")) 

c <- ggplotGrob(ggplot(head(mpg, 100), aes(class)) + geom_bar() + facet_grid(.~manufacturer, scales="free_x")) 

g1 <- rbind(a, b, size="first") 
grid.arrange(g1, c, ncol=1) 

현재 결과 : 정렬 플롯 영역 처음 두 그래픽 아니라 마지막. result plot

예상 결과 : 세 그래픽 모두의 그림 영역이 정렬됩니다.

+0

이 항목 수 도와주세요 : http://stackoverflow.com/questions/38637261/perfectly-align-several-plots – bVa

+0

감사합니다 @bVa! –

답변

2

명시 적으로 egg 패키지에서 ggarrange를 사용하여 여백 크기에 대한 걱정없이 플롯을 정렬 할 수 있습니다 (@ BVA의 링크 답변 중 하나가이 기능을 사용) :

#devtools::install_github("baptiste/egg") 
library(egg) 
library(ggplot2) 

a <- ggplot(iris[iris$Species != "setosa",], aes(x=Sepal.Length, y=Petal.Width*100000, color=Species)) + geom_line() + theme(legend.position="none") 

b <- ggplot(iris[iris$Species != "setosa",], aes(x=Sepal.Length, y=Petal.Length, color=Species)) + geom_line() + theme(legend.position="none") 

c <- ggplot(head(mpg, 100), aes(class)) + geom_bar() + facet_grid(.~manufacturer, scales="free_x") 

ggarrange(a,b,c, ncol=1) 

enter image description here

+0

이 작품은 나를 위해, 감사 eipi10 –