turning autoplot objects into a grob that can be arranged with grid.arrange
1
0
Entering edit mode
9.5 years ago
adeslatt ▴ 20

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 usable grob from the tracks t1 object so I can arrange with grid.arrange?

Anne

R • 3.9k views
ADD COMMENT
1
Entering edit mode
4.7 years ago

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

Another possibility that anybody can try is:

as(p2, 'grob')

p2 was my ggbio object.

Kevin

ADD COMMENT

Login before adding your answer.

Traffic: 2156 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6