How do I print greek letters on the diagonal of a pairs plot in R?

廉价感情. 提交于 2019-12-30 09:38:10

问题


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)

回答1:


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)

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


来源:https://stackoverflow.com/questions/1384025/how-do-i-print-greek-letters-on-the-diagonal-of-a-pairs-plot-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!