2

これがコードの例です

set.seed(1)
tmp <- matrix(replicate(4, rnorm(50)), ncol=4)
panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...)
{
  usr <- par("usr"); on.exit(par(usr))
  par(usr = c(0, 1, 0, 1))
  rp <- cor(x, y, method="pearson", use="pairwise.complete.obs")
  rs <- cor(x, y, method="spearman", use="pairwise.complete.obs")
  rp <- format(rp, digits=digits)
  rs <- format(rs, digits=digits)
  txt <- substitute(list(R[p] == rp, R[s] == rs), list(rp=rp, rs=rs))
  if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
  text(0.5, 0.5, txt, cex = 1.5)
}
panel.my.points <- function(x, y) {
  points(x, y)
  abline(0, 1)
}
pairs(tmp, 
      lower.panel=panel.cor, 
      upper.panel=panel.my.points, 
      labels=c("model 1\nD1", "model 2\nD1", "model 1\nD2", "model 2\nD2"))

これはプロットを返しますが、 ここに画像の説明を入力してください 2つの相関係数ピアソンとスピアマンを別々の線で示したいと思います。通常のテキストの場合\n、2行の出力を作成するために挿入します(対角線上のラベルのように)。相関係数を2行で出力するにはどうすればよいですか?

4

1 に答える 1

2

Pearson (txta以下のコード) と Spearman ( txtb) の係数を別々に定義してからtext2 回呼び出すのはどうでしょうか。

set.seed(1)
tmp <- matrix(replicate(4, rnorm(50)), ncol=4)
panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...)
{
  usr <- par("usr"); on.exit(par(usr))
  par(usr = c(0, 1, 0, 1))
  rp <- cor(x, y, method="pearson", use="pairwise.complete.obs")
  rs <- cor(x, y, method="spearman", use="pairwise.complete.obs")
  rp <- format(rp, digits=digits)
  rs <- format(rs, digits=digits)
  txt <- substitute(list(R[p] == rp, R[s] == rs), list(rp=rp, rs=rs))
  txta <- substitute(R[p] == rp, list(rp=rp))
  txtb <- substitute(R[s] == rs, list(rs=rs))
  if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
  text(0.5, 0.6, txta, cex = 1.5)
  text(0.5, 0.4, txtb, cex = 1.5)
}
panel.my.points <- function(x, y) {
  points(x, y)
  abline(0, 1)
}
pairs(tmp, 
      lower.panel=panel.cor, 
      upper.panel=panel.my.points, 
      labels=c("model 1\nD1", "model 2\nD1", "model 1\nD2", "model 2\nD2"))

これは与える:

ここに画像の説明を入力

于 2012-08-15T13:41:22.427 に答える