首页 文章

使用图表中的相关系数防止cex缩放 . 相关()

提问于
浏览
3

我正在尝试绘制一个巨大的相关系数矩阵,目前,我的情节看起来像这样:
Notice that some cells are missing correlation coefficients

请注意,有些单元格缺少相关系数(暂时忽略绘图缺乏对称性,除非你知道为什么会出现这种情况) . 我认为这些值实际上并没有丢失,而是显得太小而不能出现,因为它们是通过相关系数的值来缩放的 .

查看chart.Correlation()的文档,我能够找到一个函数,从中可以对chart.Correlation()的大部分内容进行建模:

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)

如果我改变:

text(0.5, 0.5, txt, cex = cex * r)

至:

text(0.5, 0.5, txt, cex = 0.8)

我大致得到了我正在寻找的效果 . 问题是我不知道如何使用chart.Correlation()本身更改此参数 . 这个问题有意义吗?

1 回答

  • 3

    这是一个修改函数的hack,允许你想要的东西 . 这会为函数添加一个用户可设置的exponent参数,允许您将 cex 值更改为 cex*[correlation]^cex.cor.scale .

    由于处理 ... 的方式,它会发出警告;他们很讨厌但无害 .

    最好联系维护者并询问他们是否愿意增强功能,或者开始创建自己的软件包修改版本 .

    edit :对相关行进行稍微更强大的更改

    library("PerformanceAnalytics")
    ## turn the function into a character string
    tmpstr <- deparse(chart.Correlation)
    ## modify the relevant lines
    panelcorline <- grep("^ *panel.cor",tmpstr)
    tmpstr[panelcorline] <- paste(tmpstr[panelcorline],"cex.cor.scale=1,")
    rscaleline <- grep("^ *text\\(0.5",tmpstr)
    tmpstr[rscaleline] <- gsub("cex \\* r","cex*r^cex.cor.scale",tmpstr[rscaleline])
    ## convert back to a function (don't mask the original function)
    my.chart.Correlation <- eval(parse(text=tmpstr))
    

    测试出来:

    data(managers)
    chart.Correlation(managers[,1:8], histogram=TRUE, pch="+")
    ## no scaling
    my.chart.Correlation(managers[,1:8], histogram=TRUE, pch="+",cex.cor.scale=0)
    ## enhanced scaling
    my.chart.Correlation(managers[,1:8], histogram=TRUE, pch="+",cex.cor.scale=2)
    

相关问题