2014-03-27 4 views
1

나는 ggplot2에 의해 생성 된 차트를 knitr 문서의 이미지 입력으로 사용하고 있습니다.R : ggplot2 축을 넘어 플롯 배경을 확장

plot

: LaTeX의 플롯 대 (링크) 오른쪽

R에 LaTeX의 차트에서 볼 수 있듯이 나는 축/그림 영역과 전설을 넘어 배경색을 확장하고자하는 R 번호 :

데이터 예 Timely Porfolio Horizon Chart

require(ggplot2) 
require(reshape2) 
require(quantmod) 
require(PerformanceAnalytics) 
require(grid) 
require(scales) 


data(edhec) 
origin = 0 

#get 12 month rolling return of edhec indexes 
roc <- as.xts(apply(cumprod(edhec+1),MARGIN=2,ROC,n=12,type="discrete"),order.by=index(edhec)) 

roc<-roc[complete.cases(roc),] 
roc.df <- as.data.frame(cbind(index(roc),coredata(roc)))[c(1:20),c(1,8,9,10)] 
colnames(roc.df)<-gsub(" |\\/","",colnames(roc.df)) 


roc.melt <- melt(roc.df,id.vars=1) 
roc.melt[,1] <- as.Date(roc.melt[,1]) 
roc.df[,1] <- as.Date(roc.df[,1]) 
names(roc.melt)[1]<-"date" 
names(roc.df)[1]<-"date" 


Rbgcol<-"#E7EAEC" 


ggplot(roc.melt,aes(x=date,y=value,color=variable))+geom_line() + scale_y_continuous(limits = c(-0.1,0.3)) + labs(title="R Plot")+ 

theme(
axis.title.x=element_blank(), 
axis.title.y=element_blank(), 
legend.position="bottom", 
legend.title=element_blank(), 
legend.background= element_rect(fill=Rbgcol, colour=NA), 
legend.key = element_rect(colour = NA, col = alpha(Rbgcol,0.1), size = .3, fill = alpha(Rbgcol,0.1)), 
panel.grid.minor.x = element_blank(), 
panel.grid.major.x = element_blank(), 
panel.grid.minor.y = element_blank(), 
axis.ticks = element_blank(), 
panel.margin = unit(0,"null"), 
plot.margin = rep(unit(0,"null"),4) 
) 

모든 포인터가 크게 도움이 될 것입니다, 감사합니다.

답변

5

theme()plot.background= 인수를 사용하여 플롯 배경의 색을 설정할 수 있습니다.

+theme(plot.background=element_rect(fill="grey90")) 
+0

+1! 플롯 영역의 기본 색상이 "grey90"입니까? – Michele

+0

아마도, 나는 아이디어를 설명하기 위해 임의의 색을 사용했습니다. –

+1

+1도! 'legend.background','legend.key','panel.background' &'strip.background' 모두 사용자 정의 스타일을 균일하게 적용하려고 할 때 매우 중요합니다. 'background_economist'에 대한 소스를'ggthemes' 패키지에서보세요. 완전한 배경색 커버리지를 보장하는 예제입니다. – hrbrmstr