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

Histograms axes limits are wrong when using geom_smooth() #9

Closed
zakahmad opened this issue May 16, 2015 · 13 comments
Closed

Histograms axes limits are wrong when using geom_smooth() #9

zakahmad opened this issue May 16, 2015 · 13 comments

Comments

@zakahmad
Copy link

Occasionally, I like to have both axes of a scatter have the same range or limits (in the original plot below I set the limits to c(-20,30)). However, to demonstrate the issue, I changed the y-axis limits using scale_y_continuous(limits = c(-40,30)) while having scale_x_continuous(limits = c(-20,30)).

The actual range of the data in the x-axis is: -17.49, 28.56 and on the y-axis is: -9.63, 17.58

I think the location of histograms should adjust and shift when the axes limits are shifted.

ggextra-issue

@daattali
Copy link
Owner

Thanks for the report. Fixing

@WilDoane
Copy link

WilDoane commented Mar 8, 2016

A version of this problem appears to still exist when geom_smooth() extends the axis range.

gg <-
    ggplot(mtcars, aes(x = mpg, y = cyl)) + 
    geom_point() 
ggMarginal(gg, type = "histogram")

mtcars2

gg <-
    ggplot(mtcars, aes(x = mpg, y = cyl)) + 
    geom_point() +
    geom_smooth()
ggMarginal(gg, type = "histogram")

mtcars

package       * version date       source        
 colorspace      1.2-6   2015-03-11 CRAN (R 3.2.2)
 devtools        1.10.0  2016-01-23 CRAN (R 3.2.3)
 digest          0.6.9   2016-01-08 CRAN (R 3.2.3)
 ggExtra       * 0.3.1   2015-11-07 CRAN (R 3.2.3)
 ggplot2       * 2.1.0   2016-03-01 CRAN (R 3.2.3)
 gridExtra       2.2.1   2016-02-29 CRAN (R 3.2.3)
 gtable          0.2.0   2016-02-26 CRAN (R 3.2.3)
 labeling        0.3     2014-08-23 CRAN (R 3.2.2)
 memoise         1.0.0   2016-01-29 CRAN (R 3.2.3)
 munsell         0.4.3   2016-02-13 CRAN (R 3.2.3)
 plyr            1.8.3   2015-06-12 CRAN (R 3.2.2)
 Rcpp            0.12.3  2016-01-10 CRAN (R 3.2.3)
 RevoUtilsMath * 3.2.3   2016-02-09 local         
 scales          0.4.0   2016-02-26 CRAN (R 3.2.3)

@daattali
Copy link
Owner

daattali commented Mar 8, 2016

Thanks for the report. I'm really busy for the next 2 months, I'll try to take a look when I can, but unfortunately this is low priority at the moment

@daattali
Copy link
Owner

@WilDoane I looked into this a bit but wasn't able to find a good solution. What I tried to do is to just copy the scale aesthestics from the main plot to the marginal plot, but for some reason it doesn't work. If anyone wants to tackle this, I'd gladly welcome a PR.

@maelle
Copy link

maelle commented Oct 19, 2016

@daattali "What I tried to do is to just copy the scale aesthestics from the main plot" -> where exactly in ggMarginal? Do you mean

        ggplot2::scale_x_continuous(breaks = mean(getLimits(pbTop, "x")),
                                    labels = ylabel)

?

I'm looking at the problem because a colleague of mine uses custom x and y limits.

@daattali
Copy link
Owner

@masalmon getLimits() and getScale() are two functions I use to try to match the original plot. If your colleague uses custom x and y limits, she should be fine, that works. Example:

ggExtra::ggMarginal(
  ggplot(mtcars, aes(mpg,wt)) +
    geom_point() + 
    scale_x_continuous(limits = c(-5, 60))
)

What doesn't work is using geom_smooth() - then the y-axis marginal plot is a bit out of place

@daattali daattali changed the title Histograms don't adjust when changing axes limits Histograms axes limits are wrong when using geom_smooth() Dec 4, 2016
@jimoeppen
Copy link

The original issue raised by zakahmad in 2015 still seems to be present, without using geom_smooth. If the default axis limits are over-ridden, the x-axis marginal plot aligns correctly with the data, but the y-axis marginal plot does not. Here is some code to illustrate the situation [R 3.4.3; ggExtra_0.7; ggplot2_2.2.1]:

df <- data.frame(x = rnorm(500, 50, 10), y = runif(500, 0, 50))
p1 <- ggplot2::ggplot(df, ggplot2::aes(x, y)) + ggplot2::geom_point() +
scale_x_continuous(limits = c(0, 150)) +
scale_y_continuous(limits = c(0, 100))
ggMarginal(p1, type = "histogram")

@crew102
Copy link
Contributor

crew102 commented Jan 11, 2018

@jimoeppen , the problem with using limits with ggMarginal that you mention is related to #81.

@jimoeppen
Copy link

jimoeppen commented Jan 11, 2018 via email

@crew102
Copy link
Contributor

crew102 commented Jan 12, 2018

Hi @jimoeppen, both axis are actually problematic. You can see both graphs in #81 for this, or just check out the below code, in which the x marginal plot is wrong and we don't even get a y marginal plot.

library(ggplot2)
library(ggExtra)

p <- ggplot(data = mtcars) +
  geom_point(aes(wt, mpg)) +
  ylim(c(25, 35))

ggMarginal(p, type = "histogram")

The bigger issue mentioned in #81 is that ggMarginal is not using the correct range for the marginal plots.

@jessiepluto
Copy link

I just encountered this same issue -- ggMariginal histograms are not lining up with the y-axis correctly after adjusting the limits. Is there any update on a solution?

@daattali
Copy link
Owner

A huge feature was just added, the fix for this is next

@daattali
Copy link
Owner

fixed

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

No branches or pull requests

7 participants