r - 使用表达式等创建多行标签

标签 r

这是代码示例

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"))

返回绘图 enter image description here 但我想在不同的行上显示两个相关系数 Pearson 和 Spearman。对于普通文本,我插入 \n 来生成两行输出(如对角线上的标签)。我怎样才能在两行中输出相关系数?

最佳答案

分别定义 Pearson(下面代码中的 txta)和 Spearman (txtb) 系数,然后调用 text 两次怎么样:

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"))

这给出:

enter image description here

关于r - 使用表达式等创建多行标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11969838/

相关文章:

r - Leaflet 在 R Markdown 中导致错误

r - 按行获取数据帧列表的平均值

r - 如何在R中的表达式中使用变量的值?

r - 如何获取不同长度列表中项目的频率

r - 使用 R 中每个数据帧的相应列名称填充数据帧列表中的列

roxygen2 不创建 .Rd 文档

使用 r 包 xlsx 删除 Excel 中的工作表

r - dput(as.Date(df$date)) 显示为数字

r - 在包装函数中从github采购R脚本以供全局 session 使用?

R CMD 检查 : Unusual Checking installed package size note