daattali / ggExtra

📊 Add marginal histograms to ggplot2, and more ggplot2 enhancements
http://daattali.com/shiny/ggExtra-ggMarginal-demo/
Other
380 stars 48 forks source link

Marginal plots scaled by row/column statistics #98

Closed scbrown86 closed 6 years ago

scbrown86 commented 6 years ago

Hi, I was wondering if it's possible to get the marginal histograms scaled by (for example), the median of each row/column in a plot? Using rastervis::levelplot it's possible to scale the marginal plots by row/column mean/median/max/min etc., this would be really helpful if it was possible to do the same thing with ggExtra.

library(ggplot2)
library(ggExtra)
library(raster)
library(rasterVis)

r <- raster(res = 20)
r[] <- runif(ncell(r))
rDF <- as.data.frame(r, xy = TRUE)

## rastervis plot with marginal density scaled by row/column medians
rasterVis::levelplot(r, margin = list(draw = TRUE, FUN = "median",
                                      scales = list(x = c(0,1), y = c(0,1)),
                                      axis = TRUE))

## ggplot of raster data
p <- ggplot(data = rDF, aes(x = x, y = y)) +
  geom_tile(aes(fill = layer)) +
  coord_equal(ylim = c(-90,90), xlim = c(-180,180), expand = FALSE) +
  theme(legend.position = "bottom", legend.direction = "horizontal")
p
## p with marginal density plots
ggMarginal(p, type = "density", margins = "both")
crew102 commented 6 years ago

Hi @sbrown-UniOfAdelaide , thanks for suggesting this new feature of ggExtra. In my mind it is beyond the scope of the package for ggMarginal to handle a tile plot. It would also require quite a bit of work to implement this, as much of the code insideggMarginal relies on the assumption that the main plot is a scatter plot.

I suggest taking a look at the gridExtra package as well as the answers described here to do more customized plots like the one you described.

daattali commented 6 years ago

@sbrown-UniOfAdelaide ggMarginal is specifically for creating marginal plots of histograms. What you are requesting may indeed be a great feature but is out of scope here. You're welcome to submit a PR to support these types of plots, but otherwise it will not get implemented. I'm closing this issue but you're welcomed to submit a PR!