lawremi / ggbio

Grid and ggplot2 based visualization for biological data
111 stars 24 forks source link

combine ggplot2 plot with ggbio plot #138

Open whui2bioinformatics opened 3 years ago

whui2bioinformatics commented 3 years ago

Hi, I had a problem when combine ggplot2 plots with ggbio plot.

here is my ploting codes fig1 <- ggplot(bw, aes(x=bw[,1], y=bw[,2])) + geom_area(fill=trackcol, alpha=1) + ylab(time) + scale_y_continuous(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0)) + coord_cartesian(ylim = c(0, ymax), xlim=c(gene.range$start-extend, gene.range$end+extend), expand = FALSE) + theme(panel.background = element_rect(fill= "white", colour = "white"),axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank(),axis.line.y.left = element_line(color = 'black'), aspect.ratio=1/10)

fig2 <- ggplot(bw, aes(x=bw[,1], y=bw[,2])) + geom_area(fill=trackcol, alpha=1) + ylab(time) + scale_y_continuous(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0)) + coord_cartesian(ylim = c(0, ymax), xlim=c(gene.range$start-extend, gene.range$end+extend), expand = FALSE) + theme(panel.background = element_rect(fill= "white", colour = "white"), axis.line.y.left = element_line(color = 'black'), aspect.ratio=1/10)

fig3 <- autoplot(anno2, which=gene.gr, label.color = "black", color = "brown", fill = "brown") + ylab('Gene') + xlab('bp')+ scale_y_continuous(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0)) + coord_cartesian(xlim=c(gene.range$start-extend, gene.range$end+extend), expand = FALSE) + theme(panel.background = element_rect(fill= "white", colour = "white"), axis.line.y.left = element_line(color = 'black'), axis.line.x.bottom = element_line(color = 'black'), aspect.ratio=1/10)

fig1 and fig2 results are as follow image

fig3 is as this image

and I want to combine these three picture exactly like this image (I manually combine these. all figures y axis are in one vertical line, and all figure have the same x axis ranges, and the x axis calibration in all figure need consistent, here the x axis calibration in third figure is not consistent with above, although I used the same x axis range, I don't know why)

but I got an error when I tryed to combine these using grid.draw(rbind(ggplotGrob(fig1), ggplotGrob(fig2),size = "last")) the error info are: Error in UseMethod("ggplot_build") : no applicable method for 'ggplot_build' applied to an object of class "c('GGbio', 'Cache')"

or using grid.arrange(fig1, fig2, ncol = 1), got error like this Error in gList(list(grobs = list(list(x = 0.5, y = 0.5, width = 1, height = 1, : only 'grobs' allowed in "gList" In addition: Warning message: In class(grob) <- c("gTableChild", class(grob)) : Setting class(x) to multiple strings ("gTableChild", "GGbio", ...); result will no longer be an S4 object

I found arrangeGrobByParsingLegend funtion from ggbip.pdf, and I got this figure image (the y axis is not in one line, picture size had changed and x axis is not consistent)

but I need the combination exactly as I mentioned above. thank you!

whui2bioinformatics commented 3 years ago

using ggplot() + geom_alignment() replace the autoplot function, and then using patchwork R package combining figure can sovle this problem

sanchit-saini commented 3 years ago

Hi @2961461560 I think it can be archived without any external package. For this purpose, ggbio provides an API to combine plots.

tracks(fig1, fig2, fig3)

Reference : https://rdrr.io/bioc/ggbio/man/tracks.html

whui2bioinformatics commented 3 years ago

OK, Thank you very much! @sanchit-saini