Ggplot2에서 여백을 변경하는 방법(예제 포함)
ggplot2의 theme() 인수를 사용하여 플롯의 여백 영역을 수정할 수 있습니다.
ggplot(df, aes(x=x)) + geom_histogram() + theme(plot. margin =unit(c(5,1,1,1), ' cm '))
플롯 여백의 순서는 다음과 같습니다.
- 단위(c(위, 오른쪽, 아래, 왼쪽), 단위)
다음 예에서는 실제로 ggplot2 플롯의 여백 영역을 변경하는 방법을 보여줍니다.
예 1: 기본 플롯 생성
다음 코드는 여백 영역을 지정하지 않고 ggplot2에서 기본 플롯을 생성하는 방법을 보여줍니다.
library (ggplot2)
#make this example reproducible
set. seeds (0)
#create data
df <- data. frame (x=rnorm(n= 5000 ))
#create histogram using ggplot2
ggplot(df, aes(x=x)) +
geom_histogram() +
ggtitle(' Title of Histogram ') +
theme(plot. background =element_rect(fill=' #e3fbff '))
플롯의 각 측면에 최소한의 여백이 있음을 확인하십시오.
예 2: 플롯 여백 변경
다음 코드는 플롯의 위쪽과 아래쪽에 상당한 여백을 추가하는 방법을 보여줍니다.
library (ggplot2)
#make this example reproducible
set. seeds (0)
#create data
df <- data. frame (x=rnorm(n= 5000 ))
#create histogram with significant margins on top and bottom
ggplot(df, aes(x=x)) +
geom_histogram() +
ggtitle(' Title of Histogram ') +
theme(plot. margin =unit(c(5,1,5,1), ' cm '),
plot. background =element_rect(fill=' #e3fbff '))
플롯의 상단과 하단에 상당한 양의 공간이 있다는 점에 유의하십시오.
다음 코드는 플롯의 왼쪽과 오른쪽에 상당한 여백을 추가하는 방법을 보여줍니다.
library (ggplot2)
#make this example reproducible
set. seeds (0)
#create data
df <- data. frame (x=rnorm(n= 5000 ))
#create histogram with significant margins on left and right
ggplot(df, aes(x=x)) +
geom_histogram() +
ggtitle(' Title of Histogram ') +
theme(plot. margin =unit(c(1,5,1,5), ' cm '),
plot. background =element_rect(fill=' #e3fbff '))
플롯의 왼쪽과 오른쪽에 많은 공간이 있다는 점에 유의하세요.
추가 리소스
다음 튜토리얼에서는 ggplot2에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.
ggplot2에서 글꼴 크기를 변경하는 방법
ggplot2에서 축 레이블을 회전하는 방법
ggplot2에서 범례를 제거하는 방법
ggplot2에서 축 레이블을 제거하는 방법