Plot ggpairs and legends (made separately) together

时间:2016-02-03 03:48:41

标签: r ggplot2 legend

I'm making a correlation plot using ggpairs and extracting legends from ggplot. Now I want to plot them together and was wondering how to do that. The source code for what I have done so far in making these plots are given below:

library(ggplot2)
library(GGally)
library(gridExtra)
library(tmle)
data(fev)

Custom Function 1

#Custom smooth function for ggpairs
my_smooth <- function(data,mapping,...){ggplot(data=data,mapping=mapping)+geom_smooth(method = "lm",se=FALSE,fullrange=TRUE)+geom_point(...)+scale_shape_manual(values=c(0,1))}

Custom Function 2

#Custom density function for ggpairs
my_density <- function(data,mapping,...){ggplot(data=data,mapping=mapping)+geom_density(...,lwd=1)}

GGpairs plot

p<-ggpairs(fev,columns=1:3,mapping=aes(color=smoke,shape=sex,lty=sex),columnLabels=c("Age","FEV","Height"),lower=list(continuous=wrap(my_smooth,alpha=0.75)),diag=list(continuous=wrap(my_density,alpha=0.5)),upper=list(continuous=wrap("cor",size=4)))+theme_bw()

Legend Extraction

#Extract Legend

g_legend<-function(a.gplot){
      tmp <- ggplot_gtable(ggplot_build(a.gplot))
      leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
      legend <- tmp$grobs[[leg]]
      return(legend)}

Legend 1

#Create some plots to extract the legend from
smoke_legend <- g_legend(ggplot(fev,aes(x=smoke,y=fev,fill=smoke))+geom_boxplot(alpha=0.75)+geom_point(aes(color=smoke),alpha=0)+guides(color=guide_legend(title="Smoke",override.aes = list(shape=15,size=10,alpha=0.75)),fill=FALSE))

Legend 2

sex_legend <- g_legend(ggplot(fev,aes(x=age,y=fev,color=smoke))+geom_point(aes(shape=sex))+geom_smooth(method="lm",se=FALSE,aes(lty=sex,shape=sex))+guides(color=FALSE,shape=guide_legend("Sex"),lty=guide_legend("Sex"))+scale_shape_manual(values = c(0,1))+theme(legend.key.height=unit(1,"cm"),legend.key.width=unit(1,"cm"),legend.key=element_rect(fill="white",color="black",size = 0.1),legend.title=element_text(size=12)))

The plots look the following:

"ggpairs image" "Legend 1"

"Legend 2"

Now I want to arrange my ggpairs plot p and legends smoke_legend and sex_legend in one plot. Is there a way to do it or is this a futile attempt?

0 个答案:

没有答案