Home > front end >  Is it possible to have two different types of marginal distribution with ggExtra::ggMarginal()?
Is it possible to have two different types of marginal distribution with ggExtra::ggMarginal()?

Time:03-09

Is it possible to have two different types of marginal distribution in enter image description here

I tried having both types in a list ( ggMarginal(p, type = list("density", "histogram"))), but that type must be of length 1.

Thanks in advance for your help!

CodePudding user response:

I think you need to create both plots and swap their upper margin grobs:

library(ggExtra)
library(ggplot2)

p <- ggplot(mtcars, aes(wt, mpg))   geom_point()

ph <- ggMarginal(p, type = "histogram")
pd <- ggMarginal(p, type = "density")

ph$grobs[ph$layout$name == "topMargPlot"] <- 
  pd$grobs[ph$layout$name == "topMargPlot"]

ph

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

  • Related