簡體   English   中英

如何在 GGally 相關圖矩陣上添加線擬合

[英]How to add line fit on GGally correlation plot matrix

下面的情節

在此處輸入圖片說明

使用以下代碼生成:

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

如何為上面的每個散點圖添加相關線?

像這樣的東西?

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

在此處輸入圖片說明

您可以在如下所示的函數中根據需要自定義下三角形中的散點圖:

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM