r - Plot ggpairs and legends (made separately) together -


i'm making correlation plot using ggpairs , extracting legends ggplot. want plot them , wondering how that. source code have done far in making these plots given below:

library(ggplot2) library(ggally) library(gridextra) library(tmle) data(fev) 

custom function 1

#custom smooth function 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 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 plots extract legend 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 following:

"ggpairs image" "legend 1"

"legend 2"

now want arrange ggpairs plot p , legends smoke_legend , sex_legend in 1 plot. there way or futile attempt?


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -