【问题标题】:Prevent cex from scaling with the correlation coefficient in chart.Correlation()防止 cex 与 chart.Correlation() 中的相关系数缩放
【发布时间】:2012-10-07 04:09:54
【问题描述】:

我正在尝试绘制一个巨大的相关系数矩阵,目前,我的绘图如下所示:

请注意,某些单元格缺少相关系数(暂时忽略绘图缺乏对称性,除非您碰巧知道为什么会出现这种情况)。我相信这些值实际上并没有丢失,只是太小而无法出现,因为它们是按相关系数的值缩放的。

查看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() 本身更改此参数。发出这个有意义吗?

【问题讨论】:

标签: r correlation


【解决方案1】:

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

由于... 的处理方式,它会发出警告;它们很烦人但无害。

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

编辑:对相关行进行更稳健的更改

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)

【讨论】:

  • Ben Bolker,非常感谢!当我尝试运行以下任一行时似乎出现错误:## no scaling my.chart.Correlation(managers[,1:8], histogram=TRUE, pch="+",cex.cor.scale= 0) ## 增强缩放 my.chart.Correlation(managers[,1:8], histogram=TRUE, pch="+",cex.cor.scale=2) 正常图表看起来很棒,但其他两个产生了一个单元格的相关图...再次感谢您的破解!我永远无法自己解决这样的问题。
  • deparse(chart.Correlation)[c(4,18)] 的结果是什么?它们看起来像可以被上面的那些明智地替换的线条吗?您使用的是最新版本(PerformanceAnalytics 1.0.4.4)(请参阅sessionInfo())?这种 hack 将对底层函数的变化非常敏感......
  • 它们看起来像您要替换的那些。并且 PerformanceAnalytics 是最新的: > ## convert back to a function (不要屏蔽原始函数) > my.chart.Correlation:4:85: 意外输入 3: x = checkData(R, method = "matrix") 4: panel.cor
  • 抱歉,我无法在评论中让它看起来更好。似乎有两个问题:1)意外的输入(所以,可能是复制和粘贴的拼写错误?)和2)它不喜欢 cex.cor.scale (不在原始函数中)......想法?
  • 没关系!我刚刚注意到您的编辑,并且有效!但我仍然对上一条评论中的逗号感到好奇。
猜你喜欢
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
相关资源
最近更新 更多