简体   繁体   中英

How to add line fit on GGally correlation plot matrix

The following plot

在此处输入图片说明

Is generated with the following code:

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

How can I add the correlation line for each of the scatter plot above?

Something like this?

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

在此处输入图片说明

You can customize the scatterplots in the lower triangle as you wish within a function like the one below:

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)))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM