[R] xy.coords in text

Xavier Robin Xavier.Robin at unige.ch
Thu Oct 23 13:58:04 CEST 2008


I want to add text annotation about correlation on "pairs" plots. I
found that I could pass a function to the "panel" argument of pairs :

  panel.annot <- function(x, y, ...) {
    points(x, y, ...)
    c <- cor.test(x, y)
    legend("topleft", legend=substitute(rho == r, list(r=sprintf("%.2f",
c$estimate))), bty="n")

And then :

  dat <- data.frame(a=rnorm(100), b=runif(100), c=runif(100)) # just
random data
  pairs(dat, panel=panel.annot)

It works fine. But what I plot is not really a legend, so I'd prefer to
use the text function instead of legend :

  panel.annot <- function(x, y, ...) {
    points(x, y, ...)
    c <- cor.test(x, y)
    text("topleft", labels=substitute(rho == r, list(r=sprintf("%.2f",

But the text is not plotted and I get warnings instead :

  1: In xy.coords(x, y, recycle = TRUE) :
    NAs introduced by coercion

If I run xy.coords("topleft") directly, I can see that the y coord (and
ylab as well) is not defined, but I don't understand why it would work
with legend and not with text... Especially since ?text doc states that

> 'y' may be missing since 'xy.coords(x,y)' is used for construction of the coordinates.

Can someone explain me this difference? And optionally how to plot text
in the "topleft" part of the plot without using legend?


Xavier Robin

Biomedical Proteomics Research Group (BPRG)
Department of Structural Biology and Bioinformatics (DBSB)
Geneva University Medical Center (CMU)
1, rue Michel Servet - CH-1211 Genève 4 - Switzerland
Tel: (+41 22) 379 53 21
Fax: (+41 22) 379 59 84
Xavier.Robin at unige.ch

More information about the R-help mailing list