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

Add marginal density/histogram to geom_hex #27

Open
mikejiang opened this issue Jul 22, 2016 · 7 comments
Open

Add marginal density/histogram to geom_hex #27

mikejiang opened this issue Jul 22, 2016 · 7 comments

Comments

@mikejiang
Copy link

It will be nice to able to show geom_hex along with two densities on top and right since there are circumstances where scatterplot is too slow due to the huge number of events.

@daattali
Copy link
Owner

You can do that right now. Just call ggMarginal() with a plot that has a geom_hex layer instead of geom_point.

library(ggplot2)
library(ggExtra)
d <- ggplot(diamonds, aes(carat, price))
ggMarginal(d + geom_hex())

@hisakatha
Copy link

For your information, I'd like to let @daattali know that geom_hex is no longer supported since ggExtra version 0.9, although there is another way to achieve similar plots (http://www.lreding.com/nonstandard_deviations/2017/08/19/cowmarg/).

Example:

(R: 3.6.1, ggplot2: 3.2.1, ggExtra: 0.9)

library(ggplot2)
library(ggExtra)
p1 <- ggplot(mtcars, aes(wt, mpg))
ggMarginal(p1 + geom_point(), type = "density")

ggMarginal(p1 + geom_hex(), type = "density")
#> Error: No geom_point layer was found in your scatter plot
ggMarginal(p1 + geom_hex(), type = "histogram")
#> Error: No geom_point layer was found in your scatter plot

Created on 2019-11-27 by the reprex package (v0.3.0)

@daattali
Copy link
Owner

Thanks. You could technically still do it with ggExtra by also adding + geom_point(col="transparent") but it's not very efficient because the geom_point takes a long time

library(ggplot2)
library(ggExtra)
d <- ggplot(diamonds, aes(carat, price))
ggMarginal(d + geom_point(col="transparent")+geom_hex())

But I'll reopen the issue since that's not a great solution

@daattali daattali reopened this Nov 29, 2019
@crew102
Copy link
Contributor

crew102 commented Nov 30, 2019

@daattali , do we want to relax the assumption that there has to be a geom_point layer for ggMarginal to try to create the marginals? In other words, do we want to check if there is either geom_point or geom_hex moving forward, as opposed to requiring there be a geom_point layer?

@daattali
Copy link
Owner

daattali commented Dec 1, 2019

I'm comfortable with that, under the assumption that it will also work correctly of course :) I tried seeing what happens if we check for point OR hex, and didn't change anything else, and the results are incorrect because the marginal density treats the hex as if they are regular points, disregarding the density that each hex point conveys.

library(ggplot2)
library(ggExtra)
d <- ggplot(diamonds, aes(carat, price))
ggMarginal(d + geom_point())

image

ggMarginal(d + geom_hex())

image

@crew102
Copy link
Contributor

crew102 commented Dec 3, 2019

Ergh, you're right. It would be possible to use the underlying data, but it would add additional complexity to the codebase and I don't think it would be worth it. I'm partial to simply requiring that a geom_point layer be used.

@daattali
Copy link
Owner

daattali commented Dec 3, 2019

Agreed. This can remain open as I'm not opposed to supporting it if a natural solution presents itself

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

4 participants