3

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:

enter image description here

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

Thanks in advance,

user3576287
  • 932
  • 3
  • 16
  • 30

3 Answers3

3

There is another library corrplot

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

enter image description here

shantanu pathak
  • 2,018
  • 19
  • 26
0

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

library(corrplot)
M <- cor(mtcars)
corrplot(M, method = "circle")
Andrelrms
  • 819
  • 9
  • 13
0

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

rbatt
  • 4,677
  • 4
  • 23
  • 41