2013-04-27 1 views
33

ggplot2에서 어떻게 범례를 반투명 배경으로 만들 수 있습니까?ggplot2 범례에서 '알파'레벨 제어하기

다음 코드는 완전히 투명 배경 (및 위치 제어)

plot <- plot + theme(legend.position=c(1,1),legend.justification=c(1,1), 
         legend.direction="vertical", 
         legend.box="horizontal", 
         legend.box.just = c("top"), 
         legend.background = element_rect(fill="transparent")) 

을 제공하지만, 하나는 알파의 수준을 제어 할 수있는 방법, 나는 element_rect 그 능력을 가지고 있다고 생각하지 않습니다.

답변

47

색상과 알파 값을 제공하여 scales 패키지의 기능 alpha()으로 반투명을 제어 할 수 있습니다. 이 함수는 fill=에 색상을 제공 할 때 element_rect()에서 사용할 수 있습니다.

library(scales)  
p<-ggplot(iris,aes(Petal.Length,Petal.Width,color=Species))+geom_point() 
p+theme(legend.position=c(1,1),legend.justification=c(1,1), 
     legend.direction="vertical", 
     legend.box="horizontal", 
     legend.box.just = c("top"), 
     legend.background = element_rect(fill=alpha('blue', 0.4))) 

enter image description here

+0

나는 것을 시도했지만 내 ggplot2 보고서 오류 "함수 알파를 찾을 수 없습니다"... ??? 최신 버전 사용, 0.9.3.1 –

+3

이 기능은 라이브러리 (스케일)입니다. 내 대답이 업데이트되었습니다. –

+0

우수. 이제 작동, 환호. –

관련 문제