Ggplot2에서 투명한 배경을 사용하는 방법
다음 구문을 사용하여 ggplot2의 플롯에 투명한 배경을 만들 수 있습니다.
p+ theme( panel. background = element_rect(fill=' transparent '), #transparent panel bg plot. background = element_rect(fill=' transparent ', color= NA ), #transparent plot bg panel. grid . major = element_blank(), #remove major gridlines panel. grid . minor = element_blank(), #remove minor gridlines legend. background = element_rect(fill=' transparent '), #transparent legend bg legend. box . background = element_rect(fill=' transparent ') #transparent legend panel )
ggsave() 를 사용하여 플롯을 내보내기로 결정한 경우 배경이 투명해야 함을 지정해야 합니다.
ggsave(' myplot.png ' , p, bg = ' transparent ' )
다음 예에서는 실제로 이 구문을 사용하는 방법을 보여줍니다.
예: ggplot2에서 투명한 배경 사용
다음 코드는 ggplot2에서 간단한 그룹화된 상자 그림을 만드는 방법을 보여줍니다.
library (ggplot2) #make this example reproducible set. seeds (1) #create dataset data <- data. frame (team=rep(c(' A ', ' B ', ' C '), each= 50 ), program=rep(c(' low ', ' high '), each= 25 ), values=seq(1:150)+sample(1:100, 150, replace= TRUE )) #create boxplot ggplot(data, aes (x=team, y=values, fill=program)) + geom_boxplot()
다음 코드를 사용하여 플롯의 투명한 배경을 만들 수 있습니다.
library (ggplot2) #make this example reproducible set. seeds (1) #create dataset data <- data. frame (team=rep(c(' A ', ' B ', ' C '), each= 50 ), program=rep(c(' low ', ' high '), each= 25 ), values=seq(1:150)+sample(1:100, 150, replace= TRUE )) #create boxplot p <- ggplot(data, aes (x=team, y=values, fill=program)) + geom_boxplot() + theme( panel. background = element_rect(fill=' transparent '), plot. background = element_rect(fill=' transparent ', color= NA ), panel. grid . major = element_blank(), panel. grid . minor = element_blank(), legend. background = element_rect(fill=' transparent '), legend. box . background = element_rect(fill=' transparent ') ) #display boxplot p
그런 다음 내보낸 이미지에서 배경이 투명해야 함을 지정하여 이 경로를 PNG 파일로 내보낼 수 있습니다.
ggsave(' grouped_boxplot.png ' , p, bg = ' transparent ' )
내보낸 이 파일을 내 컴퓨터에서 열면 배경이 투명한 것을 볼 수 있습니다.
추가 리소스
ggplot2에서 글꼴 크기를 변경하는 방법
ggplot2에서 범례를 제거하는 방법
ggplot2에서 눈금선을 제거하는 방법