【问题标题】:How to resize legend in ggpubr如何在ggpubr中调整图例的大小
【发布时间】:2021-12-06 18:04:07
【问题描述】:

我有以下代码:

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

它会生成以下图:

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

【问题讨论】:

    标签: r ggpubr


    【解决方案1】:

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

     ggarrange(legend)
    

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

    【讨论】:

      猜你喜欢
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多