如何在GGally相关图矩阵上添加线拟合

时间:2015-06-15 23:28:13

标签: r ggplot2 ggally

以下情节

enter image description here

使用以下代码生成:

library(GGally)
dat <- read.csv("http://www.ats.ucla.edu/stat/data/tobit.csv")
ggpairs(dat[, c("read", "math", "apt")])

如何为上面的每个散点图添加相关线?

2 个答案:

答案 0 :(得分:1)

这样的东西?

ggpairs(dat[, c("read", "math", "apt")],lower = list(continuous = "smooth", params = c(method = "loess", fill = "blue"))

enter image description here

答案 1 :(得分:0)

您可以在如下所示的函数中根据需要自定义下三角形中的散点图:

library(GGally)
dat <- ggplot2::diamonds[1:1000, c("x", "y", "z")]  # Example data

# Customize your scatterplots as you wish here:
lowerfun <- function(data, mapping) {
  ggplot(data = data, mapping = mapping)+ 
    geom_point(alpha = .25) + 
    geom_smooth(method = "loess", formula = y ~ x, 
                fill = "blue", color = "red", size = 0.5)
}

# Plot the scatterplot matrix
ggpairs(dat, lower = list(continuous = wrap(lowerfun)))

enter image description here