R:ggplot2扩展了超出轴的绘图背景

时间:2014-03-27 14:15:30

标签: r plot ggplot2 area

我使用ggplot2生成的图表作为knitr文档的图像输入。我希望将背景颜色扩展到轴/绘图区域和图例之外,如右侧的LaTeX图表所示(链接)

R vs LaTeX Plot:

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)
)

任何指针都会有很大帮助,谢谢。

1 个答案:

答案 0 :(得分:5)

您可以在plot.background=中使用参数theme()为剧情背景设置颜色。

 +theme(plot.background=element_rect(fill="grey90"))
相关问题