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
19 stars 5 forks source link

Misalignment of x-axis when combining gg_genetracks with another ggplot. #21

Closed VitorAguiar closed 5 months ago

VitorAguiar commented 5 months ago

Hi,

I tried combining a gg_genetracks with another ggplot with patchwork, and the x axes don't align well.

susie_tnfaip3

Then, I tried forcing the x-axis range for gg_genetracks with:

gg_genetracks(loc) + coord_cartesian(xlim = range(loc$data$pos)) + scale_x_continuous(labels = function(x) x/1e6)

Now the axis labels align well, but genes are kept on original positions, so the plot is wrong.

susie_tnfaip3-2

I'd appreciate any help, thank you!

myles-lewis commented 5 months ago

Hi Vitor,

You need to post the code you used to generate the top ggplot2 scatter plot as you have modified the legend and its position as well as axes (looks like a different ggplot2 theme), as well as the code you use to call patchwork. Without this I can't see what has caused patchwork to fail and so I would be unlikely to be able to help you much. It would be even better and more helpful if you could post a reprex (reproducible example) please, perhaps using the example data included in the package. What system/OS are you on; what version of R and what version of locuszoomr, ggplot2, patchwork etc? Are you using RStudio or standard R? Have you updated all packages to the latest versions?

Have you tried using cowplot::plot_grid() instead? I find this more stable as patchwork has to do quite a lot of work to reduce plots to objects and use symbols to link them.

The reason modifying the gg_genetracks alters the axis but not the gene track graphics is that the gene track graphics is a single grob generated using the grid package. The gg_genetracks axis needs to be left alone so that it aligns with the grob. You will have better luck modifying the alignment of the scatter plot. My guess is that the misalignment is caused because the ggplot2 theme applied to the scatter plot is different to the theme (theme_classic) applied to the gene tracks. Try applying the same ggplot2 theme to both scatter plot and gene tracks and then they'll probably line up correctly. Alternatively, it might be something to do with the legend placement.

Bw, Myles

myles-lewis commented 5 months ago

Example code:

library(EnsDb.Hsapiens.v75)
library(locuszoomr)
library(patchwork)
library(ggplot2)

data(SLE_gwas_sub)
loc <- locus(SLE_gwas_sub, gene = 'UBE2L3', flank = 1e5, LD = "r2",
             ens_db = "EnsDb.Hsapiens.v75")

p <- gg_scatter(loc)
g <- gg_genetracks(loc)

# this works: correct alignment
p / g

# move legend to outside the scatter plot
p2 <- p + theme(legend.position = "right")

# this also works: correct alignment
p2 / g
VitorAguiar commented 5 months ago

@myles-lewis Thanks for your suggestions. I have just deleted my last comment, and will continue investigating this tomorrow.

myles-lewis commented 5 months ago

Hi Vitor,

I did see you last comment before you deleted it. I think what you want to do is manually change the x axis limit rather than specify it using flank, so that you use the actual range of the points at that region. The way to do this is first to define the locus object. Then to manually alter the xrange element in it. Then call both gg_scatter and gg_genetracks. For alignment these commands rely on the x axis range as stored in loc$xrange. You can manually alter the x axis limits in gg_genetracks, but this is not a good idea as the grob of the genes won't update.

Here is an example of how to do this which maintains alignment and fixes the gene tracks themselves.

library(EnsDb.Hsapiens.v75)
library(locuszoomr)
library(patchwork)
library(ggplot2)

data(SLE_gwas_sub)
loc <- locus(SLE_gwas_sub, gene = 'UBE2L3', flank = 8e4, LD = "r2",
             ens_db = "EnsDb.Hsapiens.v75")

p <- gg_scatter(loc) + theme(legend.position = "right")
g <- gg_genetracks(loc)

# this also works: correct alignment
# but there is a gap on left hand side, caused by a region with no actual SNPs 
# on the left side of UBE2L3
p / g

# solution
loc$xrange <- range(loc$data$pos)
p <- gg_scatter(loc) + theme(legend.position = "right")
g <- gg_genetracks(loc)

p / g

Is this what you wanted?

Myles

VitorAguiar commented 5 months ago

@myles-lewis I will conduct further testing, but using

+ scale_x_continuous(limits = loc$xrange)

to build the p plot with custom ggplot2 code does seem to fix the issue.

Thank you so much!