Plot ggplot2 and ggbio Images Together
2
4
Entering edit mode
@andrewjskelton73-7074
Last seen 4 weeks ago
United Kingdom

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

ggplot2 ggbio cowplot • 4.4k views
ADD COMMENT
7
Entering edit mode
@antonio-miguel-de-jesus-domingues-5182
Last seen 11 weeks ago
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

0
Entering edit mode

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 REPLY
1
Entering edit mode

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.

 

0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

0
Entering edit mode

great, I just know the ggplot slot from ggbio. further that, Now we can combine plots using like cowplot::plot_grid()

ADD REPLY
0
Entering edit mode
Assa Yeroslaviz ★ 1.5k
@assa-yeroslaviz-1597
Last seen 3 months ago
Germany

Woulde ggarange from the ggpubr package work?

see here - https://www.r-bloggers.com/ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/

ADD COMMENT

Login before adding your answer.

Traffic: 457 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