更改图表下方对角线上的点字符。相关()

时间:2016-05-04 18:31:52

标签: r charts 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=“.”)行。具体来说,它会抛出错误,

  

点误差(x,y,pch = pch,col = col,bg = bg,cex = cex):     论证&#34; x&#34;缺少,没有默认

错误肯定是指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()语法来实现它,但我愿意接受其他解决方案。

2 个答案:

答案 0 :(得分:3)

我猜你用&#34;字处理器&#34;进行了编辑。或者将其复制为一个用smate-quotes替代常规报价的网站。不:panel.smooth(pch=“.”)。请尝试改为:

    panel.smooth(pch=".")

答案 1 :(得分:2)

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

# 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=".")

如果您正在尝试完成更复杂的事情(因此需要编辑功能本身),请澄清问题,我将更新我的答案。