2014-03-27 29 views
1

我使用由ggplot2生成的圖表作爲knitr文檔的圖像輸入。我要延長背景色以外的軸/繪圖區和圖例,作爲乳膠圖表中看到的右手側(連接)R:ggplot2擴展繪圖背景超出座標軸

řVS乳膠簡介:

plot

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'在嘗試獲得統一應用的自定義樣式時都非常重要。請在'ggthemes'包中查看'theme_economist'的來源,以確保完整的背景顏色覆蓋範圍。 – hrbrmstr