首页 > 解决方案 > 如何在ggpubr中调整图例的大小

问题描述

我有以下代码:

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

它生成以下图:

在此处输入图像描述

我想知道是否有办法在上面生成的公共图中(相对于公共图中的两个数字)调整图例的大小?“高度”功能似乎并没有调整图例本身的大小,只是调整它占用的空间量。

标签: rggpubr

解决方案


我通过后门解决了这个问题。我只是通过编写将图例分成不同的输出

 ggarrange(legend)

这使我可以将图例输出到单独的图像中,该图像可以在 rstudio 之外调整大小。


推荐阅读