Create plots dynamically in grid.arrange in R

1,727 views
Skip to first unread message

munasur12

unread,
Jun 6, 2016, 10:55:49 PM6/6/16
to ggplot2
Hi,

I am trying to dynamically create ggplots using grid.arrange. Here is the code I am running:
 

plots.vec<-list()

for(cluster.name in names(gene.cluster.color.list)){
      
     tmp.m<-X_c_normalized[rownames(gene.cluster.color.list[[cluster.name]]),]
     tmp.m.melt<-melt(tmp.m)
      
         print(cluster.name)
       p1<-ggplot()
        p1<-p1+geom_line(data=tmp.m.melt,aes(x=X2, y=as.numeric(value),group=X1))
        p1<-p1+theme(panel.background = element_rect(fill = 'white',color="black"),plot.title =element_text(size=20
                     ,face='bold'))
        p1<-p1 + labs(title = cluster.name)+ xlab("Conditions") + ylab("Normalized Gene Expression")
        
         plots.vec[cluster.name[]]<-c(p1)
 }

jpeg(filename="~/testimage.jpg",width=900,height=900)
    do.call(grid.arrange, c(plots.vec, ncol=2))
dev.off()
    

However, I get the following error:
error in glist(list(data=list(), layers=list(<environment>),scales=<environment> :
only 'grobs' allowed in "glist"

Any insight you can give would be greatly appreciated.

thanks,
Muna

Ben Bolker

unread,
Jun 6, 2016, 11:05:47 PM6/6/16
to munasur12, ggplot2
just a guess

do.call(grid.arrange, c(plots.vec, list(ncol=2))) ?
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

munasur12

unread,
Jun 7, 2016, 9:13:07 AM6/7/16
to ggplot2, anuradha...@gmail.com
Ben,

Thanks for your prompt reply. I tried
do.call(grid.arrange, c(plots.vec, list(ncol=2)))

However, I am still getting the same error. I am new to ggplot and just learned about the gridExtra package. Therefore, I am not sure if it a mistake in the ggplot,gridExtra or both.

Best,
Muna
Reply all
Reply to author
Forward
0 new messages