set.seed(1)
library(GGally)
library(hexbin)
df <- as.data.frame(matrix(rnorm(20*3), ncol=3))
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=4), y,x)
p
Using stat_binhex() with ggpairs()
Question
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!
Solution
OTHER TIPS
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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow