zoukankan      html  css  js  c++  java
  • pair correlation ggpair ggmatrix

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

     

    R 语言矩阵散点图

    散点图矩阵因包含多种多副图片,因此含有更多的信息,特别是不同变量之间相关关系。本文介绍了四种绘制散点图矩阵的函数。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)
    )

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

    ③GGally 包 ggscatmat 函数

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

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


    EasyCharts团队出品

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

    QQ交流群:454614789

    微信公众号:EasyCharts

    更多信息敬请查看: 

  • 相关阅读:
    asp之GetArray提取链接地址,以$Array$分隔的代码
    冒泡排序的优化
    Shell中, 退出整个脚本
    Shell中的算术运算(译)
    天黑了
    你猜
    2015-12-08
    Aspen
    Spring字符集过滤器CharacterEncodingFilter
    UILocalNotification ios本地推送
  • 原文地址:https://www.cnblogs.com/xiaojikuaipao/p/6078762.html
Copyright © 2011-2022 走看看