본문 바로가기
4. 그래프 꾸미기(저수준 함수)/margin - 여백

R에서 여백(margin)설정하는 방법 - 두가지 여백(inner, outer)

by makhimh 2019. 12. 4.
반응형

R에서 여백(margin)설정하는 방법


R plot 화면의 여백은 두 종류가 있습니다. inner 여백과 Outer 여백입니다. inner 여백은 데이터 표시영역과, 축과 title을 포함한 그래프영역 사이의 영역입니다. Outer 여백은 그래프영역과 전체영역 사이의 영역입니다. 


마진은 par함수 안에 정의합니다. Inner 마진은 mar=c(아래,왼쪽,위,오른쪽) 형식으로 숫자벡터를 입력하고, Outer 마진은 oma=c(아래,왼쪽,위,오른쪽) 형식으로 숫자벡터를 입력합니다. 


#Inner margin

par(mar=c(아래,왼쪽,위,오른쪽))

#Outer margin

par(oma=c(아래,왼쪽,위,오른쪽))


각 마진의 디폴트값(초기값)은 아래와 같습니다. 


#outer mar default c(0,0,0,0)

#inner mar default c(5, 4, 4, 2) + 0.1.


그림으로 나타내면 아래와 같습니다. 



> code


par(mar=c(5,5,5,5),

    oma=c(3,3,2,3))


plot(1,type="n",xlim=c(0,5),ylim=c(0,5))


box(col="red")

box("figure", col="gray")  

box("outer", col="blue")  


mtext("Inner Margin", side=3,cex=2,col="red",line=2)

mtext("Outer Margin", side=1,cex=2,col="blue",line=1, outer=TRUE)


화면을 분할하고 여러 그래프를 그릴 때는 각 그래프마다 inner 마진을 따로 설정할 수 있습니다. 



> code


par(mfrow=c(2,1),

    mar=c(5,5,5,5),

    oma=c(3,3,2,3)

)


#그래프1

plot(1,type="n",xlim=c(0,5),ylim=c(0,5))


box(col="red")

box("figure", col="gray")  


#그래프2

par(mar=c(3,3,3,3))

plot(1,type="n",xlim=c(0,5),ylim=c(0,5))


box(col="red")

box("figure", col="orange")  


box("outer", col="blue")  


반응형

댓글