本文介绍了如何在 R 中成对图的对角线上打印希腊字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 R 中创建一个配对图,在对角线上有写为希腊字母的标签.我尝试创建一个自定义 text.panel 函数,该函数将标签包装在 expression() 调用中,但这不起作用.

I want to create a pairs plot in R that has labels on the diagonal written as greek letters. I've tried creating a custom text.panel function that wraps the labels in an expression() call, but this does not work.

这是一个简单的测试用例:

Here is a simple test case:

pairs.greek <- function(x, ...)
{
  panel.txt <- function(x, y, labels, cex, font, ...)
  {
    lab <- labels
    text(0.5, 0.5, expression(lab), cex=cex, font=font)
  }
  pairs(x, text.panel=panel.txt)
}
dat <- data.frame(alpha=runif(10), beta=runif(10), gamma=runif(10))
pairs.greek(dat)

推荐答案

expression(lab) 实际上并不对 lab 求值,因此您最终会得到所有标签.相反,您可以将该行更改为:

expression(lab) doesn't actually evaluate lab so you end up with all the labels being lab. Instead, you could change that line to:

text(0.5, 0.5, parse(text=lab), cex=cex, font=font)

这会做你想做的.请注意,pairs 函数也接受一个标签参数,所以这也可以:

which will do what you want. Note that the pairs function also accepts a labels argument, so this would work too:

pairs(dat, labels=c(expression(alpha), expression(beta), expression(gamma)))

这篇关于如何在 R 中成对图的对角线上打印希腊字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 21:09