使用来自 How to reproduce smoothScatter's outlier plotting in ggplot?、R - Smoothing color and adding a legend to a scatterplot 和 How to use loess method in GGally::ggpairs using wrap function 的想法,您可以定义自己的函数以传递给 ggpairs。
my_fn <- function(data, mapping, ...){
p <- ggplot(data = data, mapping = mapping) +
stat_density2d(aes(fill=..density..), geom="tile", contour = FALSE) +
scale_fill_gradientn(colours=rainbow(100))
p
}
ggpairs(dat, lower=list(continuous=my_fn))
编辑
来自评论:如何在对角线上添加直方图并删除相关值中的“Corr:”?
您可以设置diagonal 和upper 参数。因此,要添加直方图(假设您的意思是 geom_histogram),您可以使用 diag=list(continuous=wrap("barDiag", binwidth=100)) 并完全删除相关性使用 upper=list(continuous="blank")。如果您想实际删除文本 *corr:*,您需要定义一个新函数 - 请参阅函数 cor_fun Change colors in ggpairs now that params is deprecated。
所以你的情节变成了
ggpairs(dat, lower=list(continuous=my_fn),
diag=list(continuous=wrap("barDiag", binwidth=100)),
upper=list(continuous=wrap(cor_fun, sz=10, stars=FALSE))
)
编辑
来自评论:你如何像在 OP 中那样为对角直方图着色?
要着色,只需将相关参数添加到barDiag 函数,在本例中为fill 和colour。所以diag 会是
diag=list(continuous=wrap("barDiag", binwidth=100, fill="brown", col="black"))
(fill 给出主要颜色,col 给出条形轮廓颜色)