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

Marginal boxplot to histogram not working #131

Closed peterolejua closed 6 years ago

peterolejua commented 6 years ago

Hello I want a marginal boxplot to a histogram. Like a year ago I could do it with the same code below

library(ggplot2)
library(ggExtra)

set.seed(17)

x <- rpois(10000,lambda = 2)

c <- ggplot() + 
  geom_histogram(aes(x),fill='lightblue',closed='left', bins = 10)+
  geom_vline(xintercept= quantile(x),linetype = 3,colour = "Red")+
  theme_bw()

d <- ggMarginal(c, type = "boxplot", fill="lightblue",colour='blue',margins='x',size=30)

d

image

Clearly the boxplot doesn´t correspond to what it is supposed to. I appreciate any help.

Best regards,

Peter

crew102 commented 6 years ago

Hi Peter, ggMarginal() requires that the main plot is a scatter plot (and not a histogram, as you have there). I'm not sure why/how it would have worked for you in the past.

peterolejua commented 6 years ago

Well I did it in the past like this:

qu <- quantile(estancia.uci)

c <- ggplot(data = uci, aes(estancia.uci)) + 
  geom_histogram(breaks=seq(0, 2000, by =30),fill='lightblue',closed='left')+
  xlab("# Días")+
  ylab("# Pacientes")+
  labs(colour="",
       title="Tiempo de estancia en la UCI")+
  geom_vline(data= uci, xintercept= qu,linetype = 3,colour = "Red")+
  scale_x_continuous(breaks = seq(0,2000,200))+
  scale_y_continuous(breaks = seq(0,30,2))+
  theme_bw()

d <- ggMarginal(c, type = "boxplot", fill="lightblue",colour='blue',margins='x',size=30)

tiff('estancia.tiff',width = 720)
d
dev.off()

and it gave me this:

image

It would be great if your function could to do it again,

Thank you very much!

daattali commented 6 years ago

Hi Peter, It's interesting that this used to work for you, because it was never an intended feature. The documentation and examples were always explicit in saying that this function adds marginal plots to scatter plots. I'm sorry that histograms aren't supported any longer, if I had known that they worked in the past I would have added a comment saying this behaviour may not remain forever. Unfortunately because this is meant to be a supported feature, I'm going to close this issue. I hope it's not breaking anything extremely critical for you. If you must still use this "feature", you can re-install an old version. Again, sorry for the inconvenience!