0

I'm getting error when trying to save list of lists of plots in one pdf this way:

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

where "plots" is a list of lists of 2 plots. I hope to to plot each inner list of plots in one page. I'm using ggsave because I didn't find a solution where I can control the width and height of the plots.

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

This is the error:

Error: Can't add theme to a theme object.

plotting code:

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

Any help will be greatly appreciated! Thanks!

  • In order to help you we need to see the code for the plots, as well as some data to run the code. Also, `patchwork` is the new and improved way of composing multiple plots: https://patchwork.data-imaginist.com/ – Magnus Nordmo Oct 27 '20 at 17:11
  • thanks for the prompted comment. added. – Eden Goldfarb Oct 27 '20 at 17:19

1 Answers1

0

I found a solution (sort of): Apparently I have installed devs version of ggplot2 (3.3.2.#####). Downgrading it to version 3.3.2 solved the error.