2013-03-07 4 views
6
나는 상관 행렬을 표시하려면 다음 코드가

,이 상관 행렬 플롯을 수정하는 방법은 무엇입니까?

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) 

내가 좋아하는 플롯 수정할 :

pairs(USJudgeRatings[,c(2:3,6,1,7)], 
     main="xxx", 
     pch=18, 
     col="blue", 
     cex=0.8) 
  • 이 포함로

    1. 작은 파란색 점 되세요을 대각선에있는 항목의 히스토그램 (enter link description here에서 볼 수 있음)

    2. 값되지 별

      r=0.9; 
      p=0.001; 
      

    과의 상관 관계 및 p- 값을 표시합니다.

    페어링 된 데이터의 산점도에 맞는 선이 표시됩니다. 피팅에 사용되는 방법은 무엇입니까? 위의 코드로 피팅이 정의 된 라인은 무엇입니까? 그리고 피팅 방법을 바꾸는 방법?

  • +0

    많이 묻지 만 시도한 것을 표시하지 않습니다. 나는 당신이 격자 포장 안에 이것을하기 위하여 운을 더 가지고 있다고 생각한다. '? splom'을 참조하십시오. – agstudy

    +0

    @agstudy 죄송합니다. 저는 R 언어를 처음 접했습니다. 어떻게해야할지 모르겠습니다. 나는 쌍을 시도했다. (USJudgeRatings [, c (2 : 3,6,1,7)], lower.panel = panel.smooth, upper.panel = panel.cor, pch = 18, col = "blue" 약간의 오류. –

    +1

    페어링 된 데이터의 산점도에 맞는 라인이 표시됩니다. 피팅에 사용되는 방법은 무엇입니까? 위의 코드로 피팅이 정의 된 라인은 무엇입니까? 그리고 피팅 방법을 바꾸는 방법? –

    답변

    33

    기능 설명 페이지 pairs()에는 플롯 할 패널을 정의하는 방법의 예가 나와 있습니다. 특정 사례를 들어

    :

    텍스트의 라인에 보여 panel.cor() 기능을 변경 - P-값과 상관 계수. panel.smooth() 함수의

    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) 
    } 
    

    cex=, col=pch= 인자를 정의.

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

    enter image description here

    +0

    감사합니다! –

    +0

    누군가가'pairs()'호출에서'cex.cor' 변수를 전달하는 방법을 알고 있습니까? 필자는이 함수가'text()'의'panel.cor()'함수에서 사용되었다고 추측합니다.하지만 추가하면 많은 경고가 발생합니다! – MikeRSpencer

    0

    수정 된 산점도 매트릭스 : 히스토그램을 추가하려면

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

    , panel.hist() 기능이 정의되어야한다

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

    최종 플롯 (pairs()의 도움말 파일에서 가져온)

    1. %% 히스토그램에 대한 수정 된 기능; panel.smooth

      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. %% 수정 회귀 함수; panel.cor

      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. %% 수정 상관 함수;

    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) 
    } 
    

    는 산점도 행렬을 플롯 할 수 있으려면, 당신은 또한 "굴림"글꼴을 설치해야합니다. 이를 수행하려면 아래 단계를 따르십시오.

    1. %% 모든 글꼴을 RStudio에 설치하십시오. 플롯의 품질을 향상시키는 것이 중요합니다!

      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. %% 마지막으로, pairs 기능을 사용하여 그림을 그릴;

      pairs(qq1, lower.panel=panel.smooth, upper.panel=panel.cor ,diag.panel=panel.hist, cex = 2, cex.labels = 2, cex.main = 2) 
      
    3. %% 최종 제품을 확인하십시오. enter image description here

    관련 문제