I would like to use the stat_binhex()
statistic from ggplot2
with the ggpairs()
function (GGally
R package). For example, I would like to use stat_binhex()
in this plot instead of geom_point()
. Is that possible?
Thanks for your help!
Cannot believe that this is still not part of the GGally
's ggpair()
's customisation.
Building on lukeA's answer, let us convert that to a function:
Setup requirements and define the function:
require(ggplot2)[enter image description here][1]
require(GGally)
require(hexbin)
ggpairs_hex <- function(df, hexbins = 10) {
# REF: https://stackoverflow.com/questions/20872133/using-stat-binhex-with-ggpairs
p <- ggpairs(df, lower="blank")
seq <- 1:ncol(df)
for (x in seq)
for (y in seq)
if (y>x)
p <- putPlot(p, ggplot(df, aes_string(x=names(df)[x],y=names(df)[y])) + stat_binhex(bins = hexbins), y,x)
return(p)
}
Generate some data:
require(MASS)
# data generation from:
# https://predictivehacks.com/how-to-generate-correlated-data-in-r/
set.seed(312)
# create the variance covariance matrix
sigma<-rbind(c(1,-0.8,-0.7), c(-0.8,1, 0.9), c(-0.7,0.9,1))
# create the mean vector
mu<-c(10, 5, 2)
# generate the multivariate normal distribution
df<-as.data.frame(MASS::mvrnorm(n=10000, mu=mu, Sigma=sigma))
Test the function:
ggpairs_hex(df, hexbins = 5)
ggpairs_hex(df, hexbins = 10)
ggpairs_hex(df, hexbins = 20)
With the current version of GGally
(2.1.2) this is much easier than it used to be: doesn't require any low-level hacking, and can more easily be combined with other aspects of ggpairs
customization.
ggally_hexbin <- function (data, mapping, ...) {
p <- ggplot(data = data, mapping = mapping) + geom_hex(...)
p
}
ggpairs(df,
## use 'ggally_hexbin' for continuous × continuous plots
lower = list(continuous = "hexbin",
## use default plots for all other variable types
combo = "facethist", discrete = "facetbar", na = "na"))