daattali / ggExtra

📊 Add marginal histograms to ggplot2, and more ggplot2 enhancements
http://daattali.com/shiny/ggExtra-ggMarginal-demo/
Other
383 stars 45 forks source link

Adding axis to marginal denstity plot and histograms #59

Closed SebastianoCurreli closed 7 years ago

SebastianoCurreli commented 7 years ago

Great package, anyway I think it could be useful to have the possibility to show the axis of the density plot/histogram, in order to be able to compare distributions of subsets of data (see pict):

example_1

As you can see the maximum of each the density distribution is the same, therefore it's not possible to appreciate the differences in density value. Thanks in advance. Sebastiano

crew102 commented 7 years ago

Can you provide a reproducible example of how you created that plot? To my knowledge, ggMarginal does not even have the ability to overlay marginal plots like the graph you displayed.

SebastianoCurreli commented 7 years ago

Sure, I'm not using the original dataset because it's quite big. An example with some simulated data is as follows:

`set.seed(1234)
n = 500;
x1 = rbeta(n, .5, .4) y1 = rnorm(n, 5, 2);
y2 = rnorm(n, 10, 1) w = rbinom(n, 1, .5)
x2 = wy1 + (1-w)y2

df_ex1<-data.frame(x = x1, y = x2) df_ex2<-data.frame(x = y1, y = y2)

p1 <- ggplot(df_ex1, aes(x, y)) + geom_point(color = "red") + theme_cowplot() + xlim(0, 10) + ylim(0,15)

p2 <- ggplot(df_ex2, aes(x, y)) + geom_point(color = "blue") + theme_cowplot() + xlim(0, 10) + ylim(0,15)

marg1<-ggMarginal(p1, color = "red") marg2<-ggMarginal(p2, color = "blue")

ggdraw()+ draw_plot(marg1) + draw_plot(marg2)`

example_ggexra_1

What I'm interested in is to be able to visualize the "y" axis of each marginal plot in order to evaluate the differences in density distribution.

Thanks in advance!

crew102 commented 7 years ago

Ah, I see what you are saying. This would require passing theme options into ggMarginal, as well as some additional changes to the function itself which would not be easy. I can point you to where in the source code you would have to make changes if you want.

SebastianoCurreli commented 7 years ago

That would be great! I'll wait for your input then.

crew102 commented 7 years ago

Take a look at some of the changes I made in this commit: 8f5e4ed72c823947e83372028b723a893d543ed1. When you rebuild ggExtra with these new changes and run the following:

library(ggplot2)
library(ggExtra)

ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = wt)) +
  ylab("\nmpg") + # need to give extra space for labs with "\n"
  xlab("\nwt") -> p

ggMarginal(p = p, type = "density")

...You should get something like this:

rplot

The alignment of the plots is nice in the example above, but you may have to change the params I've hard-coded to make the alignment work for your specific plot. I've made notes by the diff view on Github here, to help you understand what I was doing.

I don't think we would want to incorporate the ability to customize the marginal plots to the extent that you need, so I would consider this issue closed. @daattali , your thoughts?

daattali commented 7 years ago

Thank you Chris for your discussion I completely agree, this kind of customization is out of scope for the package


Dean Attali President & CEO AttaliTech Ltd http://AttaliTech.com http://attalitech.com

On 17 May 2017 at 14:13, Chris Baker notifications@github.com wrote:

Take a look at some of the changes I made in this commit: 8f5e4ed https://github.com/daattali/ggExtra/commit/8f5e4ed72c823947e83372028b723a893d543ed1 When you rebuild ggExtra with these new changes and run the following:

library(ggplot2) library(ggExtra)

ggplot(data = mtcars) + geom_point(aes(x = mpg, y = wt)) + ylab("\nmpg") + # need to give extra space for labs with "\n" xlab("\nwt") -> p

ggMarginal(p = p, type = "density")

...You should get something like this:

[image: rplot] https://cloud.githubusercontent.com/assets/11220793/26168687/0203b860-3b09-11e7-9abf-c5ccf3154081.png

The alignment of the plots is nice in the example above, but you may have to change the params I've hard-coded to make the alignment work for your specific plot. I've made notes by the diff view on Github here https://github.com/crew102/ggExtra/commit/8f5e4ed72c823947e83372028b723a893d543ed1#diff-2d2dc5c77893c0b4d033117477af9abeR283, to help you understand what I was doing.

I don't think we would want to incorporate the ability to customize the marginal plots to the extent that you need, so I would consider this issue closed. @daattali https://github.com/daattali , your thoughts?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/daattali/ggExtra/issues/59#issuecomment-302179837, or mute the thread https://github.com/notifications/unsubscribe-auth/AA6IFJwVmgDlNYpcIQ1-zIgwsH14HBTJks5r6zjWgaJpZM4NcZb1 .

SebastianoCurreli commented 7 years ago

Thank you Chris for your prompt help!