Question: turning autoplot objects into a grob that can be arranged with grid.arrange
0
gravatar for adeslatt
5.8 years ago by
adeslatt20
United States
adeslatt20 wrote:

Hi there,

I want to arrange several graphical objects side by side -- using say autoplot and barcharts next to them.

Grid.arrange works fine -- but autoplot does not print 

c1 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar()
c2 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar() + coord_flip()
grid.arrange(c1,c2,ncol=1) works great.

Now, looking at the objects that autoplot likes to work with -- usually genomic segments.

set.seed(123)
gr.b <- GRanges(seqnames = "chr1", IRanges(start = seq(1, 100, by = 10),
                  width = sample(4:9, size = 10, replace = TRUE)),
                  score = rnorm(10, 10, 3), value = runif(10, 1, 100))
gr.b2 <- GRanges(seqnames = "chr2", IRanges(start = seq(1, 100, by = 10),
                  width = sample(4:9, size = 10, replace = TRUE)),
                  score = rnorm(10, 10, 3), value = runif(10, 1, 100))
gr.b <- c(gr.b, gr.b2)
p1 <- autoplot(gr.b, geom = "bar")
p2 <- autoplot(gr.b, geom = "bar", aes(fill = value))
t1 <- tracks(default = p1, fill = p2)

t1 will plot it

Tracks then arranges the two genomic centric plots in proportional view in the window.

You can unlist(t1) this If you inspect the autoplot object (which is arranged on a genomic axis typically) you can see that it is a graphical object --   

If you aren't familiar with ggbio  -- it is a very nice package for displaying genomic tracks -- I just want to put some abundance information side by side my transcript information -- just seeing what others do .....

In any case -- thoughts on how I can make a useable grob from the tracks t1 object so I can arrange with grid.arrange?

Anne

grob autoplot R grid.arrange • 2.4k views
ADD COMMENTlink modified 12 months ago by Kevin Blighe63k • written 5.8 years ago by adeslatt20
0
gravatar for Kevin Blighe
12 months ago by
Kevin Blighe63k
Kevin Blighe63k wrote:

Hey,

I stumbled upon this question because I was facing the same issue today.

Here is the error message that I was getting while trying to plot a ggbio object via plot_grid() (cowplot) and grid.arrange() (grid)

no method or default for coercing "GGbio" to "grob"

The solution was to simply access the 'ggplot' slot / object of the ggbio object, i.e.:

grid.arrange(
  p1,
  p2@ggplot,
  ncol = 1,
  top = textGrob("", gp=gpar(fontsize=42, fontface="bold")),
  layout_matrix = cbind(c(1,2,2,2)))

As possibility that anybody can try is:

as(p2, 'grob')

p2 was my ggbio object.

Kevin

ADD COMMENTlink written 12 months ago by Kevin Blighe63k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 711 users visited in the last hour