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

parameters for marginal plots (xparams) not going through: 'boundary', 'center' for histogram #164

Closed PatrickOStats closed 2 years ago

PatrickOStats commented 2 years ago

With 'xparams' and 'yparams' parameters can fed to, e.g. the marginal histograms. However, it appears that 'boundary' is already fixed internally and providing a value is ignored. Likewise, if instead trying to specify only the option 'center' (no 'boundary'), an error is produced that both can't be specified simulatenously. I've not explored extensively whether other options in x/yparams would also produce errors/be ignored.

Could you please fix this if it's not too much trouble?

Why care?

Minimum reproducible example

library(ggplot2)
library(ggExtra)
set.seed(1)
data <- data.frame("y" = runif(n = 100, min = 0, max = 100),
                   "x" = sample(x = 1:5, size = 100, replace = TRUE,
                                prob = c(0.1, 0.1, 0.2, 0.2, 0.4)))
 p <- ggplot(data, aes(x, y)) +
  geom_point() + xlim(0, 6)

# option 'boundary' has no effect, but no message thrown. 
(p2 <- ggMarginal(p = p, type = 'histogram', 
                  xparams = list(boundary = 0.5, binwidth = 1)))
(p2 <- ggMarginal(p = p, type = 'histogram', 
                   xparams = list(boundary = 0, binwidth = 1)))
(p2 <- ggMarginal(p = p, type = 'histogram', 
                   xparams = list(boundary = 1.25, binwidth = 1)))

# option 'center' produces an error.  
(p2 <- ggMarginal(p = p, type = 'histogram', 
                   xparams = list(center = 1, binwidth = 1)))
# Error: Only one of `boundary` and `center` may be specified. 
# ... even though only 'center' was specified, but 'boundary' was not.

# Luckily, option 'breaks' works... if xlim is speficied appropiately
(breaks.seq <- seq(from = min(data$x) - 0.5, by = 1L,
                  length.out = max(data$x) - min(data$x) + 2))
(p2 <- ggMarginal(p = p, type = 'histogram', 
                  xparams = list(breaks = breaks.seq, binwidth = 1)))
daattali commented 2 years ago

Thanks for the detailed report @PatrickOStats - fixed by @crew102