How to modify this Correlation Matrix plot?

匆匆过客 提交于 2019-11-28 19:55:20

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)

Modified Scatter Plot Matrix

  1. %% Modified function for histogram;

    panel.hist <- function(x, ...)
    {
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(usr[1:2], 0, 1.5) )
    par(cex.axis=2, family="Times New Roman", face="bold", size=12, cex.lab=1, cex.main=1, cex.sub=1)
    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", ...)
    
    }
    
  2. %% Modified Regression Function with panel.smooth;

    panel.smooth<-function (x, y, col = "black", bg = NA, pch = 16, 
                    cex = 2, 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, ...)
    }
    
  3. %% Modified Correlation Function with panel.cor;

    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), cex = 2.5, family="Times New Roman", face="bold", size=12)
    text(.5, .75, Signif, cex = 2.5, family="Times New Roman", face="bold", size=12)
    }
    

To be able to plot the scatterplot matrix, you also need to install "Times New Roman" font. To do it, follow the steps below;

  1. %% Install all fonts into RStudio. This is important to improve the quality of the plot!

    install.packages("extrafont") # Install fonts
    library(extrafont)            # Install library
    font_import()                 # Import all fonts  
    loadfonts(device="win")       # Register fonts for Windows bitmap output
    fonts()                       # Finish the process
    
  2. %% Finally, plot your figure with pairs function;

     pairs(qq1, lower.panel=panel.smooth, upper.panel=panel.cor ,diag.panel=panel.hist, cex = 2, cex.labels = 2, cex.main = 2)
    
  3. %% Check the final product; enter image description here

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