我正在调整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)中的错误:参数"x“缺失,没有缺省值
这个错误肯定是指那个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()语法来实现这一点,但对其他解决方案是开放的。
发布于 2016-05-11 17:08:28
如果我误解了您的问题,但如果您只是想让对角线左下角的散点图使用点,请原谅我,那么下面的代码将为您做到这一点:
# 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=".")如果你试图完成更复杂的事情(因此需要编辑函数本身),请澄清这个问题,我会更新我的答案。
发布于 2016-05-09 23:51:16
我猜你是用“文字处理器”编辑的,还是把它复制到一个用smate-引号代替常规引号的站点上。不是:panel.smooth(pch=“.”)。试一试:
panel.smooth(pch=".")https://stackoverflow.com/questions/37035373
复制相似问题