How to resize legend in ggpubr

Viewed 158

I have the following code:

T<-as.data.frame(matrix(0,11,2))
T$V2<-T$V2+1
colors <- c("V1" = "blue", "V2" = "red")
A<-ggplot(T,aes(x=seq(-5,5)))+
  #geom_smooth(aes(ymin=Treat-Treatse,ymax=Treat+Treatse),colour='blue')+
  geom_line(aes(y=V1,color="V1"))+
  geom_point(size=2,aes(y=V1,color="V1"))+
  geom_line(aes(y=V2,color="V2"))+
  geom_point(size=2,aes(y=V2,color="V2"))+
  labs(x='Yadayada',y='Dumdedum',color="Legend")+
  scale_color_manual(values = colors)+geom_vline(xintercept=0,linetype='dashed',col='blue')+
  scale_x_continuous(breaks=seq(-5,5),labels=c('-5','-4','-3','-2','-1','0','1','2',
                                               '3','4','5'))+theme_classic()+
  ggtitle('Example')
B<-ggplot(T,aes(x=seq(-5,5)))+
  #geom_smooth(aes(ymin=Treat-Treatse,ymax=Treat+Treatse),colour='blue')+
  geom_line(aes(y=V1,color="V1"))+
  geom_point(size=2,aes(y=V1,color="V1"))+
  geom_line(aes(y=V2,color="V2"))+
  geom_point(size=2,aes(y=V2,color="V2"))+
  labs(x='Yadayada',y='Dumdedum',color="Legend")+
  scale_color_manual(values = colors)+geom_vline(xintercept=0,linetype='dashed',col='blue')+
  scale_x_continuous(breaks=seq(-5,5),labels=c('-5','-4','-3','-2','-1','0','1','2',
                                               '3','4','5'))+theme_classic()+
  ggtitle('Example')
get_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<-get_legend(B)
ggarrange(arrangeGrob(A+theme(legend.position="none"),
                      B+theme(legend.position="none")
                      ,nrow=1),legend, 
          nrow=2,heights=c(10,2))

It generates the following plot:

enter image description here

I was wondering if there was a way to resize the legend within the common plot(relative to the two figures in the common plot) that is generated above? The 'heights' function doesn't seem to resize the legend itself, merely the amount of space it takes up.

1 Answers

I solved the problem through the back door. I just separated the legend into a different output by writing

 ggarrange(legend)

This allowed me to output the legend into a separate image, which can be resized outside of rstudio.

Related