0

尝试以这种方式将绘图列表保存在一个 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))),
                               aes(x=mz,y=intensity_perc,ymin=0,ymax=intensity_perc,color=col),
                               fatten = 3)+
      
      ggrepel::geom_text_repel(data = merged_spec[[2]],
                               aes(x=mz,y=intensity_perc,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))),
                        aes(x=mz,y=intensity_perc,color=col))+
      
      ggplot2::geom_pointrange(data = (merged_spec[[1]] %>% 
                                         dplyr::filter(!is.na(ion))),
                               aes(x=mz,y=intensity_perc,ymin=0,ymax=intensity_perc,color=col), fatten = 3)+
      
      ggrepel::geom_text_repel(data = (merged_spec[[1]] %>% 
                                         dplyr::filter(!is.na(ion))),
                               aes(x=mz,y=intensity_perc,color=col,label=ion),
                               show.legend = F,size=3.5,nudge_y = 0.1,
                               #arrow = arrow(angle = 45,type = "closed",ends = "first",length = unit(2,"mm")),
                               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,max(rbind(syn_ions,endo_ions)$mz)+5),breaks = seq(100,1200,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",x = ((max.mz - min.mz)/2)-200,y=-1.4,size=5)+
      ggplot2::annotate(geom = "text",label="Endogenous", x = -Inf, y=Inf, hjust = -0.4, vjust =  4.7,size=5)+
      ggplot2::annotate(geom = "text",label="Synthetic",x = -Inf, 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)],y=-1.4,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)],y=1.4,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]],
                            aes(x=startx,y=starty,
                                xend = endx, yend = endy, color = col))+
      
      ggplot2::geom_segment(data = annotations[[3]],
                            aes(x=startx,y=starty,
                                xend = endx, yend = endy, color = col))+
      ggplot2::annotate(geom = "text",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]],
                            aes(x=startx,y=starty,
                                xend = endx, yend = endy, color = col))+
      # Endo anno
      ggplot2::geom_segment(data = annotations[[5]],
                            aes(x=startx,y=starty,
                                xend = endx, yend = endy, color = col))+
      ggplot2::annotate(geom = "text",label = annotations[[5]]$label,
                        x = annotations[[5]]$label_posx,y = annotations[[5]]$label_posy,
                        color = colory,size = 2.5)+
      
      ggplot2::geom_segment(data = annotations[[6]],
                            aes(x=startx,y=starty,
                                xend = endx, yend = endy, color = col))+
      ggplot2::geom_segment(data = annotations[[7]],
                            aes(x=startx,y=starty,
                                xend = endx, yend = endy, color = col))+
      ggplot2::annotate(geom = "text",label = annotations[[7]]$label,
                        x = annotations[[7]]$label_posx,y = annotations[[7]]$label_posy,
                        color = colorb,size = 2.5)+
      
      ggplot2::geom_segment(data = annotations[[8]],
                            aes(x=startx,y=starty,
                                xend = endx, yend = endy, color = col))

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

4

1 回答 1

0

我找到了一个解决方案(有点):显然我已经安装了 ggplot2 的开发版本(3.3.2.#####)。将其降级到版本 3.3.2 解决了该错误。

于 2020-10-27T18:05:09.733 回答