Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ggMarginal ignores expand argument #145

Closed
wpetry opened this issue Jun 21, 2019 · 9 comments
Closed

ggMarginal ignores expand argument #145

wpetry opened this issue Jun 21, 2019 · 9 comments

Comments

@wpetry
Copy link

wpetry commented Jun 21, 2019

In ggMarginal, the marginal plots use expand = TRUE even if the user has specified p to have expand = FALSE. This leads to a misalignment of the data between the main plot and margins.

@daattali
Copy link
Owner

Can you please add a small fully reproducible example code

@wpetry
Copy link
Author

wpetry commented Jun 21, 2019

Sure thing, @daattali

Load packages & generate data

library(tidyverse)
library(ggExtra)

theme_set(theme_bw() + theme(panel.grid = element_blank()))

set.seed(503234)
dat <- tibble(x = rbeta(5000, shape1 = 5, shape2 = 1),
              y = exp(rnorm(5000, mean = 0, sd = 2)))

Using the default expand = TRUE works—the density plots cut off in line with the data. This is expected behavior.

p <- ggplot(dat, aes(x = x, y = y))+
  geom_point()+
  scale_y_log10()+
  coord_cartesian(xlim = c(0, 1), expand = TRUE)
ggMarginal(p, type = "density")

Rplot

Using expand = FALSE misaligns the density plots. The lines should extend the whole axis extents, and this is particularly visible in the apparent zero density around x=1. This is unexpected behavior.

q <- ggplot(dat, aes(x = x, y = y))+
  geom_point()+
  scale_y_log10()+
  coord_cartesian(xlim = c(0, 1), expand = FALSE)
ggMarginal(q, type = "density")

Rplot01

I was able to fix it very crudely by adding a logical argument called expand to the function and replacing top[["coordinates"]][["expand"]] <- expand and right[["coordinates"]][["expand"]] <- expand. This clearly isn't the right way to do it, otherwise I'd send in a PR. Ideally an automated check of the original plot p would be run (including custom adjustments, e.g., scale_x_continuous(expand = c(0.5, 1)), and the top and right marginal plots would be adjusted accordingly.

Thanks for maintaining this great package.

@daattali
Copy link
Owner

Great, thanks for the detailed report and examples. I'm on mobile and rushing, but is this related/duplicate of #128 ?

@wpetry
Copy link
Author

wpetry commented Jun 21, 2019

They're very closely related for sure (sorry for not referencing #128), but I think this may be a separate (or subset) of that issue that would be easier to fix.

@daattali
Copy link
Owner

daattali commented Jun 21, 2019 via email

@wpetry
Copy link
Author

wpetry commented Jun 22, 2019

I thought I had a solution for the CRAN version, but I see that the GH version has switched to R6. I'm afraid this is a bit out of my depth.

@crew102
Copy link
Contributor

crew102 commented Jun 25, 2019

I'd be happy to integrate your solution into the current codas of ggExtra, so long as the fix you're talking about isn't too hairy.

@crew102
Copy link
Contributor

crew102 commented Jun 29, 2019

Hi @wpetry , I looked into this and it basically amounts to #128. ggMarginal() currently doesn't handle the cartesian coordinate system well (i.e., it doesn't work well for plots that were built using the coord_cartesian() function). The reason why it seemed like there was a specific issue with the expand argument is b/c the coord_cartesian() call in your example didn't actually change the look of the plot. If you change the values for xlim in your example, you'll see that ggMarginal() fails to account for the cartesian coordinate system, even when expand = TRUE. In other words, the problem isn't the expand argument per se, but rather the inability of ggMarginal() to account for coord_cartesian().

library(ggplot2)
library(ggExtra)

set.seed(503234)
dat <- data.frame(
  x = rbeta(5000, shape1 = 5, shape2 = 1),
  y = exp(rnorm(5000, mean = 0, sd = 2))
)

p <- ggplot(dat, aes(x, y)) +
  geom_point() +
  scale_y_log10() +
  coord_cartesian(xlim = c(0, .5), expand = TRUE)

ggMarginal(p)

Created on 2019-06-29 by the reprex package (v0.2.0.9000).

@daattali
Copy link
Owner

Thanks for looking into it. Closing this as duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants