在 chart.Correlation() 的下对角线更改点字符

Change point characters in lower diagonal of chart.Correlation()

我正在调整 PerformanceAnalytic 的 chart.Correlation() 函数。

chart.Corr = function (R, histogram = TRUE, method = c("pearson", "kendall", 
    "spearman"), ...) 
{
    x = checkData(R, method = "matrix")
    if (missing(method)) 
        method = method[1]
    panel.cor <- function(x, y, digits = 2, prefix = "", use = "pairwise.complete.obs", 
        method, cex.cor, ...) {
        usr <- par("usr")
        on.exit(par(usr))
        par(usr = c(0, 1, 0, 1))
        r <- cor(x, y, use = use, method = method)
        txt <- format(c(r, 0.123456789), digits = digits)[1]
        txt <- paste(prefix, txt, sep = "")
        #print(txt)
        strwidth(txt)
        if (missing(cex.cor)) 
            cex <- 1.5 #/strwidth(txt)
        test <- cor.test(x, y, method = method)
        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 * (abs(r) + 0.3)/1.3)
        text(0.5, 0.5, txt, cex = cex)
        text(0.7, 0.7, Signif, cex = cex-0.25, col = 1) #col = 2
    }
    f <- function(t) {
        dnorm(t, mean = mean(x), sd = sd.xts(x))
    }
    hist.panel = function(x, ...) {
        par(new = TRUE)
        hist(x, col = "light gray", probability = TRUE, axes = FALSE, 
            main = "", breaks = "FD", pch=".")
        lines(density(x, na.rm = TRUE), col = "blue", lwd = 1)
        rug(x)
    }
    print(x)
    print(class(x))
    if (histogram) 
        pairs(x, gap = 0, lower.panel = panel.smooth(pch=“.”), upper.panel = panel.cor, 
            diag.panel = hist.panel, method = method, ...) #(pch=".")
    else pairs(x, gap = 0, lower.panel = panel.smooth, upper.panel = panel.cor, 
        method = method, ...)
}

当我实例化时,它不喜欢我的 lower.panel = panel.smooth(pch=“.”) 行。具体来说,它会抛出错误

Error in points(x, y, pch = pch, col = col, bg = bg, cex = cex) : argument "x" is missing, with no default

错误肯定是指 lower.panel 行。在函数的原始语法中,没有任何参数传递给 panel.smooth(),它运行顺利:

function (R, histogram = TRUE, method = c("pearson", "kendall", 
    "spearman"), ...) 
{
    x = checkData(R, method = "matrix")
    if (missing(method)) 
        method = method[1]
    panel.cor <- function(x, y, digits = 2, prefix = "", use = "pairwise.complete.obs", 
        method, cex.cor, ...) {
        usr <- par("usr")
        on.exit(par(usr))
        par(usr = c(0, 1, 0, 1))
        r <- cor(x, y, use = use, method = method)
        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, method = method)
        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 * (abs(r) + 0.3)/1.3)
        text(0.8, 0.8, Signif, cex = cex, col = 2)
    }
    f <- function(t) {
        dnorm(t, mean = mean(x), sd = sd.xts(x))
    }
    hist.panel = function(x, ...) {
        par(new = TRUE)
        hist(x, col = "light gray", probability = TRUE, axes = FALSE, 
            main = "", breaks = "FD")
        lines(density(x, na.rm = TRUE), col = "red", lwd = 1)
        rug(x)
    }
    if (histogram) 
        pairs(x, gap = 0, lower.panel = panel.smooth, upper.panel = panel.cor, 
            diag.panel = hist.panel, method = method, ...)
    else pairs(x, gap = 0, lower.panel = panel.smooth, upper.panel = panel.cor, 
        method = method, ...)
}

我正在尝试更改图表下对角线散点图中的点字符。我更愿意通过如上所述调整 panel.smooth() 语法来做到这一点,但我对其他解决方案持开放态度。

我猜你是用 "word-processor" 编辑的,或者是从一个用 smate-quotes 代替常规引语的网站复制的。不:panel.smooth(pch=“.”)。请尝试:

    panel.smooth(pch=".")

如果我误解了你的问题,请原谅我,但如果你只是想让对角线左下角的散点图使用一个点,那么这段代码将为你做到这一点:

# Loading example data from PerformanceAnalytics
data(managers)

# running chart.Correlation with points set to "." (using the argument pch=".")
chart.Correlation(managers[,1:8], histogram=TRUE, pch=".")

如果您试图完成更复杂的事情(因此需要编辑函数本身),请澄清问题,我会更新我的答案。