尝试使用marrangeGrob和ggsave保存图列表时出错

问题描述

尝试以一种方式将绘图列表保存为一个pdf时出现错误

ggplot2::ggsave("plots.pdf",gridExtra::marrangeGrob(grobs = unlist(plots,recursive = F),nrow=2,ncol=1),device = "pdf",units = "in",width = 8,height = 13)

其中“图”是2个图的列表。 我希望在一页上绘制每个内部图列表。 我之所以使用ggsave,是因为我没有找到可以控制绘图宽度和高度的解决方案。

> length(plots)
[1] 13
> length(plots[[1]])
[1] 2

这是错误

错误:无法将theme添加主题对象。

绘图代码

ggplot()+
      ggplot2::geom_col(data = (merged_spec[[2]] %>% 
                                  dplyr::filter(is.na(ion))),aes(x=mz,y=intensity_perc,color=col)) +
      
      ggplot2::geom_pointrange(data = (merged_spec[[2]] %>% 
                                         dplyr::filter(!is.na(ion))),ymin=0,ymax=intensity_perc,color=col),fatten = 3)+
      
      ggrepel::geom_text_repel(data = merged_spec[[2]],color=col,label=ion),show.legend = F,size=3.5,nudge_y = -0.1,#arrow = arrow(type = "closed",ends = "first",length = unit(2,"mm")),segment.linetype = 3)+
      
      ggplot2::geom_col(data = (merged_spec[[1]] %>% 
                                  dplyr::filter(is.na(ion))),color=col))+
      
      ggplot2::geom_pointrange(data = (merged_spec[[1]] %>% 
                                         dplyr::filter(!is.na(ion))),fatten = 3)+
      
      ggrepel::geom_text_repel(data = (merged_spec[[1]] %>% 
                                         dplyr::filter(!is.na(ion))),nudge_y = 0.1,#arrow = arrow(angle = 45,type = "closed",segment.linetype = 3)+
      
      ggplot2::geom_hline(yintercept = 0)+
      
      #xlim(min(rbind(syn_ions,endo_ions)$mz)-5,max(rbind(syn_ions,endo_ions)$mz)+5)+
      ggplot2::scale_color_manual(values=c(colorb,colnoise,colory))+
      #ggplot2::scale_x_continuous(breaks = seq(100,1200,50))+
      ggplot2::scale_y_continuous(breaks = seq(-1,1,0.2),expand = expansion(mult = 0.09))+
      ggplot2::scale_x_continuous(limits = c(min(rbind(syn_ions,endo_ions)$mz)+5),breaks = seq(100,100))+
      
      ggplot2::labs(x= "m/z",y="Intensity (norm)",title = MS2Peptide,subtitle = paste(method,"=",abs(round(corr,4))))+
      
      ggplot2::theme_minimal(base_size = 18) +
      ggplot2::theme(panel.grid = element_blank(),panel.border = element_rect(fill=NA,size = 0.001),axis.ticks=element_line(size = 0.001))+
      ggplot2::guides(color=F,label=F,fill=F)+
      
      # annotate(geom = "text",label="Endogenous",x = ((max.mz - min.mz)/2)-200,y=1.4,size=5)+
      # annotate(geom = "text",label="Synthetic",y=-1.4,size=5)+
      ggplot2::annotate(geom = "text",x = -Inf,y=Inf,hjust = -0.4,vjust =  4.7,y=-Inf,hjust = -0.8,vjust =  -3.9,size=5)+
      
      ggplot2::annotate(geom = "text",label=pepAnno,x = seq(((max.mz - min.mz)/2)+100,((max.mz - min.mz)/2)+450,15)[1:length(pepAnno)],size=5)+
      # Synthetic anno
      ggplot2::geom_segment(data = annotations[[1]],aes(x=startx,y=starty,xend = endx,yend = endy,color = col))+
      ggplot2::annotate(geom = "text",label = annotations[[1]]$label,x = annotations[[1]]$label_posx,y = annotations[[1]]$label_posy,color = colory,size = 2.5)+
      
      ggplot2::geom_segment(data = annotations[[2]],color = col))+
      
      ggplot2::geom_segment(data = annotations[[3]],label = annotations[[3]]$label,x = annotations[[3]]$label_posx,y = annotations[[3]]$label_posy,color = colorb,size = 2.5)+
      
      ggplot2::geom_segment(data = annotations[[4]],color = col))+
      # Endo anno
      ggplot2::geom_segment(data = annotations[[5]],label = annotations[[5]]$label,x = annotations[[5]]$label_posx,y = annotations[[5]]$label_posy,size = 2.5)+
      
      ggplot2::geom_segment(data = annotations[[6]],color = col))+
      ggplot2::geom_segment(data = annotations[[7]],label = annotations[[7]]$label,x = annotations[[7]]$label_posx,y = annotations[[7]]$label_posy,size = 2.5)+
      
      ggplot2::geom_segment(data = annotations[[8]],color = col))

任何帮助将不胜感激! 谢谢!

解决方法

我找到了一种解决方案: 显然我已经安装了ggplot2的开发人员版本(3.3.2。#####)。将其降级到3.3.2版即可解决该错误。