gridGraphics::grid.echo error: EXPR must be a length 1 vector

957 views Asked by At

I'm trying to use gridGraphics::grid.echo according to this link so I can combine a Gviz plotTracks plot with a ggplot using cowplot's plot_grid.

Following Gviz's vignette and this link, here's what I do:

require(Gviz)
data(geneModels)
gtrack <- GenomeAxisTrack()
itrack <- IdeogramTrack(genome = "hg19", chromosome = as.character(geneModels$chromosome[1]))
grtrack <- GeneRegionTrack(geneModels, genome = "hg19",chromosome = as.character(geneModels$chromosome[1]), name = "Gene Model")

require(gridGraphics)

gwrap_plot <- function(x) {
  tree <- grid::grid.grabExpr(gridGraphics::grid.echo(x))
  u <- grid::unit(1, 'null')
  gtable::gtable_col(NULL, list(tree), u, u)
}

graphics.off()
plotTracks(list(itrack, gtrack, grtrack))
track.plot <- recordPlot()
gwrap_plot(track.plot)

And I get this error:

 Error in switch(x[[2]][[1]]$name, C_abline = C_abline(x[[2]]), C_plot_new = C_plot_new(x[[2]]),  : 
  EXPR must be a length 1 vector 

Any idea what's the problem of gridGraphics's grid.echo with Gviz's plotTracks plot?

1

There are 1 answers

0
baptiste On BEST ANSWER

grid.echo is for base graphics, Gviz appears to use grid graphics,

p1 = grid::grid.grabExpr(plotTracks(list(itrack, gtrack, grtrack), add = TRUE))
p2 = ggplot2::qplot(1:10, 1:10)

gridExtra::grid.arrange(p1, p2, ncol=2)

enter image description here