如何在R中生成我的data.frame的相关图?
这可能是一个简单的问题。 我有一个df,我想为我在R中的数据生成一个相关图如何在R中生成我的data.frame的相关图?,r,plot,correlation,pearson,R,Plot,Correlation,Pearson,这可能是一个简单的问题。 我有一个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') c
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方法得出的结果:
我想知道是否有另一个包可以生成我不知道的相同的相关性图
提前感谢,尝试corrplot库,下面是一个示例
库(corrplot)
M您应该查看?pairs
。它非常适合于为变量组合绘制散点图,但可以改变屏幕上显示的图形类型
下三角板(将取2个变量的函数传递到下三角板
参数)
- e、 例如,散点图的
对(df,下部面板=点
对角线(将取1个变量的函数传递到diag.panel
或text.panel
参数)
- 这很棘手!请参阅帮助文件了解如何制作直方图
上部三角形(向上部面板
参数传递2个变量的函数)
- e、 例如,
,但见下文对(df,upper.panel=函数(x,y)文本(0,0,cor(x,y))
?对):
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)
我喜欢这个函数,我自己也会使用它。它可能看起来有点奇怪,只有一个x
和一个y
,但是
试试corrlY软件包
您可以使用数据帧绘制相关矩阵。此外,您还可以使用各种方法查找相关
参见示例-
corrlY软件包的相关方法
正确安装
# install.packages("devtools")
devtools::install_github("maheshKulkarniX/corrlY")
library(plotly)
library(corrlY)
使用Kendall方法的相关系数
# Example:
corr_coef_kendall(variable1= mtcars$cyl, variable2=mtcars$carb, decimal = 2)
## [1] 0.47
# Example:
corr_coef_pearson(variable1= mtcars$disp, variable2=mtcars$hp, decimal = 2)
## [1] 0.79
# Example:
corr_coef_spearman(variable1= cars$speed, variable2=cars$dist, decimal = 2)
## [1] 0.83
皮尔逊法相关系数
# Example:
corr_coef_kendall(variable1= mtcars$cyl, variable2=mtcars$carb, decimal = 2)
## [1] 0.47
# Example:
corr_coef_pearson(variable1= mtcars$disp, variable2=mtcars$hp, decimal = 2)
## [1] 0.79
# Example:
corr_coef_spearman(variable1= cars$speed, variable2=cars$dist, decimal = 2)
## [1] 0.83
使用斯皮尔曼方法的相关系数
# Example:
corr_coef_kendall(variable1= mtcars$cyl, variable2=mtcars$carb, decimal = 2)
## [1] 0.47
# Example:
corr_coef_pearson(variable1= mtcars$disp, variable2=mtcars$hp, decimal = 2)
## [1] 0.79
# Example:
corr_coef_spearman(variable1= cars$speed, variable2=cars$dist, decimal = 2)
## [1] 0.83
使用corrlY绘制相关矩阵图
用corrlY绘制相关图
x还有另一个库corrplot
library(corrplot)
cor.table = cor(df)
corrplot(cor.table)
是的,但也许您可以解释一下当前绘图的问题是什么?