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

Allow users to pass in custom `boundary` or `center` param #166

Closed crew102 closed 2 years ago

crew102 commented 2 years ago

Previously we were always overwriting the boundary param for histogram/densigrams so that we avoided the awkward scenario where a user would want a marginal histogram but wouldn't set an xlim on their scatter plot. Something like that would result in the bins towards the low/high end of the data getting cut. So, for example, if we remove this line:

https://github.com/daattali/ggExtra/blob/808faafe7619ee43693fd8e87e81ddb714adee46/R/ggMarginal-MarginalPlot.R#L152

...we would end up with:

library(ggplot2)
library(ggExtra)

p <- ggplot(mtcars,aes(x = cyl, y = carb)) + geom_point()

ggMarginal(p, type = "histogram")

Created on 2022-03-02 by the reprex package (v2.0.1)

In #164 , Patrick made a good point re: why this was a problematic design choice. This PR should fix the issue, in that it allows people to pass in center/boundary params now. Now we can do something like this:

library(ggplot2)
library(ggExtra)

p <- ggplot(mtcars, aes(x = cyl, y = carb)) +
  geom_point() +
  xlim(0, 10)

ggMarginal(
  p,
  type = "histogram", 
  xparams = list(center = 0, binwidth = 0.5),
  yparams = list(boundary = 8, binwidth = 1),
)

Created on 2022-03-02 by the reprex package (v2.0.1)

daattali commented 2 years ago

Thanks a lot for the fix @crew102 -- I removed the shiny app change and will put that in a separate commit