Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify this Correlation Matrix plot?

I have the following codes to display a correlation matrix,

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 <- paste(prefix, txt, sep="")
    if(missing(cex.cor)) cex <- 0.8/strwidth(txt)

    test <- cor.test(x,y)
    # borrowed from printCoefmat
    Signif <- symnum(test$p.value, corr = FALSE, na = FALSE,
                  cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
                  symbols = c("***", "**", "*", ".", " "))

    text(0.5, 0.5, txt, cex = cex * r)
    text(.8, .8, Signif, cex=cex, col=2)
}
pairs(USJudgeRatings[,c(2:3,6,1,7)],
  lower.panel=panel.smooth, upper.panel=panel.cor)

I want to modify the plot like:

  1. Have smaller blue dots as

    pairs(USJudgeRatings[,c(2:3,6,1,7)],
          main="xxx",
          pch=18,
          col="blue",
          cex=0.8)
    
  2. Include a histogram of the entries on the diagonal (as seen in enter link description here)

  3. Display the correlation and p-value as

    r=0.9;
    p=0.001;
    

with values not stars.

There is a fitting line displayed for the scatter plot of the paired data. What is the method used for the fitting? Which line is defined the fitting as the codes shown above? And how to change the fitting method?

like image 546
Samo Jerom Avatar asked Mar 07 '13 12:03

Samo Jerom


People also ask

How do you format a correlation matrix?

A simple function to format the correlation matrix Column 1 : row names (variable 1 for the correlation test) Column 2 : column names (variable 2 for the correlation test) Column 3 : the correlation coefficients. Column 4 : the p-values of the correlations.

How do you increase the size of a correlation plot?

The correlation coefficient value size in correlation matrix plot created by using corrplot function ranges from 0 to 1, 0 referring to the smallest and 1 referring to the largest, by default it is 1. To change this size, we need to use number. cex argument.

How do I change the color of a correlation matrix in python?

This can be done using Matplotlib Colormaps, which can be added in the function matshow with the argument cmap=plt. get_cmap(name) .


1 Answers

Help page for the function pairs() gives you example how to define panels to plot.

For your particular case:

Changed panel.cor() function to show to lines of text - p-values and correlation coefficients.

panel.cor <- function(x, y, digits=2, 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]
  test <- cor.test(x,y)
  Signif <- ifelse(round(test$p.value,3)<0.001,"p<0.001",paste("p=",round(test$p.value,3)))  
  text(0.5, 0.25, paste("r=",txt))
  text(.5, .75, Signif)
}

For panel.smooth() function defined cex=, col= and pch= arguments.

panel.smooth<-function (x, y, col = "blue", bg = NA, pch = 18, 
                        cex = 0.8, col.smooth = "red", span = 2/3, iter = 3, ...) 
{
  points(x, y, pch = pch, col = col, bg = bg, cex = cex)
  ok <- is.finite(x) & is.finite(y)
  if (any(ok)) 
    lines(stats::lowess(x[ok], y[ok], f = span, iter = iter), 
          col = col.smooth, ...)
}

To add histograms, panel.hist() functions should be defined (taken from help file of pairs())

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", ...)
}

Final plot:

pairs(USJudgeRatings[,c(2:3,6,1,7)],
          lower.panel=panel.smooth, upper.panel=panel.cor,diag.panel=panel.hist)

enter image description here

like image 123
Didzis Elferts Avatar answered Oct 28 '22 16:10

Didzis Elferts