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 plots with other geoms for the main plot #143

Closed crsh closed 5 years ago

crsh commented 5 years ago

Hi Dean,

as per our brief Twitter conversation, here's the example where ggMarginal() doesn't work when applied to something other than a scatterplot. As I said, I'm not sure this is a bug; I was under the impression that other geoms are simply not supported.

library("ggplot2")
library("ggExtra")

contour_plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  stat_density_2d() +
  guides(color = FALSE, alpha = FALSE) +
  theme()

ggMarginal(contour_plot, type = "histogram", groupColour = TRUE, groupFill = TRUE)
Error: No geom_point layer was found in your scatter plot

If I add invisible points, it works:

contour_plot2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(alpha = 0) +
  stat_density_2d() +
  guides(color = FALSE, alpha = FALSE) +
  theme()

ggMarginal(contour_plot2, type = "histogram", groupColour = TRUE, groupFill = TRUE)
daattali commented 5 years ago

Ah yes, the code does explicitly look for a scatterlpot since that's technically what it's meant to support. If it works for other plot types, that's a nice surprise