How to generate correlation plot of my data.frame in R?

那年仲夏 提交于 2019-12-13 19:41:04

问题


It might be a simple question. I have a df and I want to generate a correlation plot for my data in 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

I've found a package called corPlot and I've generated two plots based on pearson & spearman methods.

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

here is my output with pearson method:

I wondered if there is another package to generate the same correlation plots that I am not aware of?

Thanks in advance,


回答1:


Try the corrplot library, here is an example from this link

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



回答2:


You should check out ?pairs. It's great for doing scatterplots for combinations of variables, but varying the type of graph that is displayed on the

  1. lower triangle (pass function taking 2 variables to lower.panel argument)
    • e.g., pairs(df, lower.panel=points for a scatter plot
  2. diagonal (pass function taking 1 variable to diag.panel or text.panel argument)
    • this is tricky! see help file for how to do a histogram
  3. upper triangle (pass function taking 2 variables to upper.panel argument)
    • e.g., pairs(df, upper.panel=function(x,y)text(0,0,cor(x,y))), but see below

Scatter plot and correlation, from help file (?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)

With your data, do the following:

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

I love that function, and use it myself as-is. It might look a little odd with only an x and a y, but




回答3:


Try the corrlY Package

You can plot the correlation matrix using your data frame. Also, you can find a correlation using various methods.

see the example-

Methods of Correlation Using corrlY Package

Install Corrly

# install.packages("devtools")
devtools::install_github("maheshKulkarniX/corrlY")
library(plotly)
library(corrlY)

correlation Coefficient using Kendall Method

# Example: 
corr_coef_kendall(variable1= mtcars$cyl, variable2=mtcars$carb, decimal = 2) 
## [1] 0.47

correlation Coefficient using Pearson Method

 # Example: 
    corr_coef_pearson(variable1= mtcars$disp, variable2=mtcars$hp, decimal = 2)
    ## [1] 0.79

correlation Coefficient using spearman Method

# Example:
corr_coef_spearman(variable1= cars$speed, variable2=cars$dist, decimal = 2)
## [1] 0.83

correlation Matrix Plot Using corrlY

matrixly(data = mtcars)

Correlation Plot using corrlY

x <- c(0.10967469, 1.06814661, 0.71805993, 0.60566332, 0.73714006, -0.06029712)
y <- c(1, 93, 46, 84, 12, 5)
xy <- data.frame(x, y)
spearman<- corr_coef_spearman(variable1= xy$x, variable2=xy$y, decimal = 2)
corr_scatterly(data=xy,x= xy$x, y= xy$y,corr_coef=spearman, xname="x", yname="y")

For More Information visit the GitHub repository Data Visualization Package For All Types of Correlation Charts using Plotly Package.

For More examples visit corrlY Package Site corrlY package site



来源:https://stackoverflow.com/questions/31968022/how-to-generate-correlation-plot-of-my-data-frame-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!