pair correlation ggpair ggmatrix

时间:2024-01-16 16:39:08

https://zhuanlan.zhihu.com/p/23400450

pair correlation ggpair ggmatrix

R 语言矩阵散点图

pair correlation ggpair ggmatrixEasyCharts· 15 天前

散点图矩阵因包含多种多副图片,因此含有更多的信息,特别是不同变量之间相关关系。本文介绍了四种绘制散点图矩阵的函数。graphics(pairs),car( spm),GGally(ggscatmat), GGally(ggpairs)四种。

① graphics 包 pairs 函数

data(trees)
panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- abs(cor(x, y))
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste0(prefix, txt)
if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor * r)
}
panel.hist <- function(x, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(usr[1:2], 0, 1.5) )
h <- hist(x, plot = FALSE)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- y/max(y)
rect(breaks[-nB], 0, breaks[-1], y, col = "cyan", ...)
}
pairs(trees, 
labels = c('树周长','树高','树体积'),
lower.panel=panel.cor,
upper.panel=panel.smooth,
diag.panel=panel.hist,
main = 'The Scatterplot Matrix of The Trees Data',
pch = 21,
bg = "orange",
line.main=1.5,
oma=c(2,2,3,2)
)

pair correlation ggpair ggmatrix

② car 包 spm 函数

#diagonal=c("density", "boxplot", "histogram", "oned", "qqplot", "none")
library(car)
data("trees")
spm(trees,
diagonal='histogram',
main='The Scatterplot Matrix of The Trees Data')

pair correlation ggpair ggmatrix

③GGally 包 ggscatmat 函数

library(GGally)
#ggscatmat(trees)
data("iris")
ggscatmat(iris, columns = 1:4,color = 'Species' )

pair correlation ggpair ggmatrix

④ GGally 包 ggpairs函数

library(ggplot2)
library(GGally)
data(tips,package="reshape")
#pairs(tips[,1:3])
pm1 <- ggpairs(
tips[,c(1,3,4,2)],
mapping = ggplot2::aes(color = sex,shape=sex,alpha = 0.4),
upper = list(continuous = "cor", combo = "box", discrete = "facetbar"),
lower = list(continuous = "points", combo = "facethist", discrete = "facetbar"),
diag = list(continuous = "densityDiag", discrete = "barDiag")
)
pm1

pair correlation ggpair ggmatrix

data(iris)
pm2 <- ggpairs(
iris[,c(1,2,5,3,4)],
mapping = ggplot2::aes(color = Species,shape=Species,alpha = 0.4),
upper = list(continuous = "density", combo = "box"),
lower = list(continuous = "points", combo = "dot"),
title = "Iris",
axisLabels ="internal")
pm2

pair correlation ggpair ggmatrix

data(diamonds, package="ggplot2")
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1], 200), ]
# Custom Example
pm3 <- ggpairs(
diamonds.samp[, 1:5],
mapping = ggplot2::aes(color = cut),
upper = list(continuous = wrap("density", alpha = 0.5), combo = "box"),
lower = list(continuous = wrap("smooth", alpha = 0.3), combo = wrap("dot", alpha = 0.4)),
title = "Diamonds"
)
pm3

pair correlation ggpair ggmatrix

EasyCharts团队出品

帅的人都关注了EasyCharts团队^..^~

QQ交流群:454614789

微信公众号:EasyCharts

更多信息敬请查看: http://easychart.github.io/post/Easycharts/