myles-lewis / locuszoomr

A pure R implementation of locuszoom for plotting genetic data at genomic loci accompanied by gene annotations.
GNU General Public License v3.0
18 stars 5 forks source link

stacking gg_scatter plots on top of one another #5

Closed nickhir closed 10 months ago

nickhir commented 11 months ago

Hello,

I am currently running a colocalization analysis between two traits. For this, I like to stack the plots on top of one another. This works perfectly with the example in your vignette:

library(locuszoomr)
data(SLE_gwas_sub)  ## limited subset of data from SLE GWAS
library(EnsDb.Hsapiens.v75)

loc <- locus(gene = 'UBE2L3', SLE_gwas_sub, flank = 1e5, ens_db = "EnsDb.Hsapiens.v75")
set_layers(2)
scatter_plot(loc)
scatter_plot(loc)
genetracks(loc)

However, in my case, I need to use gg_scatter instead of the scatter_plot option, because I need to do some slight modifications to my plot (I want to highlight certain points with circles and this is just much easier to do with a ggplot object). The problem that I am running into is, that when I have two ggplot objects, I cant properly align them above the gene tracks... Here is my code:

library(patchwork)
p1 <- gg_scatter(loc)
p2 <- gg_scatter(loc)
genes <- gg_genetracks(loc)
wrap_plots(p1,p2,genes, ncol=1)

Do you have any ideas how to accomplish this? I think it would be incredibly helpful because it would very much increase the flexiblity of the package.

myles-lewis commented 11 months ago

Hi nickhir,

Good question. Not immediately doable but it might be possible to modify gg_addgenes to pull the correct viewpoint coords from a reference ggplot object and use that to align the gene tracks. I will investigate and post a patch if I can get it to work.

Bw, Myles

myles-lewis commented 10 months ago

Hi nickhir, Here is some code which is a patch method using gggrid package. It's a bit rough, but seems to work. I will work on including a function to make this smoother in the next version of the package.

# new method to add gene tracks
p <- gg_scatter(loc)
g1 <- gg_genetracks(loc)

g2 <- ggplot(data.frame(x = NA),
             aes(xmin = loc$xrange[1] / 1e6, xmax = loc$xrange[2] / 1e6)) +
  gggrid::grid_panel(g1) +
  theme_classic() +
  theme(axis.text = element_text(colour = "black"),
        axis.line.y = element_blank())

library(cowplot)
plot_grid(p, p, g2, nrow = 3, rel_heights = c(2, 2, 1),
          align = "v")

# comparison
locus_ggplot(loc)
nickhir commented 10 months ago

Thank you so much for your quick action! This will save me a ton of time!

myles-lewis commented 10 months ago

Hi nickhir,

I pushed an update to the main branch of locuszoomr (now version 0.1.4). It can be installed from github:

devtools::install_github("myles-lewis/locuszoomr")

I have updated gg_genetracks() so that the functionality you described in your original post should work using patchwork. Or using plot_grid from cowplot:

p <- gg_scatter(loc)
g <- gg_genetracks(loc)
library(cowplot)
plot_grid(p, p, g, ncol = 1, rel_heights = c(2, 2, 1), align = "v")

Bw, Myles

nickhir commented 10 months ago

This is just brilliant and exactly what I was looking for! Thank you very much for being lightning fast with this!

myles-lewis commented 7 months ago

Hi Vitor,

gg_genetracks does return a ggplot object which contains an embedded grid object. Are you sure you are using the correct and latest version of locuszoomr? Can you provide a reprex (reproducible example) with a 'locus' object please.

library(patchwork)
p / p / g
wrap_plots(p, p, g, ncol = 1)

The above code used to work fine with patchwork, but I can only guess that there's a new version of ggplot2 which broke this somehow. This is a patchwork or ggplot2 bug as I got the following error:

Error in Ops.data.frame(guide_loc, panel_loc) : 
  '==' only defined for equally-sized data frames

I think it's related to https://github.com/thomasp85/patchwork/issues/342. For me it was fixed by installing the latest version of patchwork.

Or why not use cowplot instead as in the comments further above. cowplot::plot_grid works just fine.

Bw, Myles

VitorAguiar commented 7 months ago

Hi Myles,

sorry about that, I actually decided to delete my question because I realized that it works in Rstudio.

However, in my neovim + nvim-R setup in a remote server, when I execute g <- gg_genetracks(loc) I get an Error due to no connection to a X11 graphics window, so I cannot get the g object.

Yes, I'm using the latest version of locuszoomr installed from GitHub.

Thank you, Vitor

myles-lewis commented 7 months ago

Hey no problem ;-) gg_genetracks() was converted from the base graphics version genetracks() but still uses base graphics strwidth() to detect the width of text in the gene names. It’s not clear to me how to switch entirely to grid. That might be why it requires an X11 graphics window. Beyond that, I have no idea!