Question: Plot ggplot2 and ggbio Images Together
3
gravatar for andrew.j.skelton73
3.9 years ago by
United Kingdom
andrew.j.skelton73310 wrote:

Hi, 

I'm pretty stumped and wonder if anyone has any suggestions. I've got 3 plots: 1 x ggplot2 with linear x-scale, 1 x ggplot2 with a factorial x scale, and 1 x ggbio image with a linear x-scale. 

I'd like to plot these images on 3 rows in the plotting window, which seems to be a lot harder than I thought. The closest I've got to it is getting the ggbio image and the ggplot2 image with linear scale together using the tracks() function. I've tried CowPlot and grid.arrange, both of which don't seem to be able to handle ggbio objects. 

Any other suggestions would be welcome! 

Cheers

ggbio ggplot2 cowplot • 1.8k views
ADD COMMENTlink modified 3.9 years ago by António Miguel de Jesus Domingues430 • written 3.9 years ago by andrew.j.skelton73310
Answer: Plot ggplot2 and ggbio Images Together
5
gravatar for António Miguel de Jesus Domingues
3.9 years ago by
Germany

I am going out on a limb here, but since in essence all of those plots are ggplot, could this work?

Update, now with working code:

 

First I created a ggbio simple plot. Using str(p.ideo) one can see in which slot the ggplot object is, and retrieve it (I love str):

library("ggbio")

load(system.file("data", "hg19IdeogramCyto.rda", package="biovizBase", mustWork=TRUE))
p.ideo <- plotIdeogram(hg19IdeogramCyto, "chr1")
p.gg <- p.ideo@ggplot

Next I generated 2 simple ggplots:

dat <- data.frame(
  time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
  total_bill = c(14.89, 17.23)
)

p.bar <- ggplot(data=dat, aes(x=time, y=total_bill)) +
    geom_bar(stat="identity")

p.line <- ggplot(data=dat, aes(x=time, y=total_bill, group=1)) +
    geom_line()

And simply create the function and plot them together:

# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

multiplot(p.bar, p.line, p.gg, cols=1)

As a test, plotting the original ggbio object does indeed take over the plotting window:

multiplot(p.bar, p.line, p.ideo, cols=1)

 

sources:

ggbio

R cookbook

ADD COMMENTlink modified 3.9 years ago • written 3.9 years ago by António Miguel de Jesus Domingues430

Unfortunately not, it renders the first two plots (ggplot2 objects), then the ggbio plot takes over and displays across the whole window. Thanks for the suggestion though

ADD REPLYlink written 3.9 years ago by andrew.j.skelton73310
1

Could it be because the print call of ggbio is updating the plotting window? I have updated my answer with code that will plot 2 ggplots + 1 ggbio in 3 rows.

 

ADD REPLYlink modified 3.9 years ago • written 3.9 years ago by António Miguel de Jesus Domingues430

Did not know there was an @ggplot accessor method for ggbio objects! Thanks for that, solves a lot of problems! 

ADD REPLYlink written 3.9 years ago by andrew.j.skelton73310

I didn't know either until I started poking around with str().

ADD REPLYlink written 3.9 years ago by António Miguel de Jesus Domingues430
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 16.09
Traffic: 225 users visited in the last hour