i想将the
stat_binhex()
统计量与
ggplot2
函数(
ggpairs()
r软件包)一起使用。 例如,我想在this情节中使用
GGally
而不是
stat_binhex()
。这可能吗?
感谢您的帮助!
geom_point()
GGally
自定义的其他方面更容易结合。
ggpairs
cannot认为,这仍然不是
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"))
在
lukea的答案上建造,让我们将其转换为一个函数:
设置要求并定义功能:
ggpair()
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)
}
测试函数:
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))