【问题标题】:How to generate correlation plot of my data.frame in R? [closed]如何在 R 中生成我的 data.frame 的相关图? [关闭]
【发布时间】:2022-02-24 10:09:07
【问题描述】:

这可能是一个简单的问题。 我有一个 df,我想在 R 中为我的数据生成一个相关图。

head(df)

            x  y
1 -0.10967469  1
2  1.06814661 93
3  0.71805993 46
4  0.60566332 84
5  0.73714006 12
6 -0.06029712  5

我找到了一个名为 corPlot 的包,并根据 pearson 和 spearman 方法生成了两个图。

corPlot(df, method = 'pearson')
corPlot(df, method = 'spearman')

这是我使用 pearson 方法的输出:

我想知道是否有另一个包可以生成我不知道的相同相关图?

提前致谢,

【问题讨论】:

  • 是的,但也许你能解释一下你对当前情节的问题是什么?

标签: r plot correlation pearson


【解决方案1】:

还有一个库 corrplot

library(corrplot)
cor.table = cor(df)
corrplot(cor.table)

【讨论】:

    【解决方案2】:

    试试 corrplot 库,这里有一个来自 link 的示例

    library(corrplot)
    M <- cor(mtcars)
    corrplot(M, method = "circle")
    

    【讨论】:

      【解决方案3】:

      您应该查看 ?pairs。它非常适合为变量组合绘制散点图,但可以改变显示在图表上的图表类型

      1. 下三角(将 2 个变量传递给 lower.panel 参数的函数)
        • 例如,pairs(df, lower.panel=points 用于散点图
      2. diagonal(将 1 个变量传递给 diag.paneltext.panel 参数的函数)
        • 这很棘手!请参阅帮助文件了解如何制作直方图
      3. 上三角(将 2 个变量传递给 upper.panel 参数的函数)
        • 例如pairs(df, upper.panel=function(x,y)text(0,0,cor(x,y))),但见下文

      散点图和相关性,来自帮助文件 (?pairs):

      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)
      }
      pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel = panel.cor)
      

      使用您的数据,执行以下操作:

      pairs(df, lower.panel=plot, upper.panel=panel.cor)
      

      我喜欢这个功能,并按原样使用它。只有xy 可能看起来有点奇怪,但是

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-20
        • 1970-01-01
        • 1970-01-01
        • 2013-04-06
        • 1970-01-01
        • 1970-01-01
        • 2017-06-06
        • 1970-01-01
        相关资源
        最近更新 更多