[R] xy.coords in text
Xavier Robin
Xavier.Robin at unige.ch
Fri Oct 24 10:14:51 CEST 2008
Dear Greg,
So it appears I was wrongly (not surprising) thinking "toplef" was a
xy.coords behaviour, when it is only a legend quirk. I didn't know about
the grconvert(X|Y) functions, and they do a very nice job indeed!
So I ended up with the following:
panel.annot <- function(x, y, ...) {
points(x, y, ...)
c <- cor.test(x, y)
text(grconvertX(0.01, "npc"), grconvertY(.99, "npc"),
labels=substitute(rho == r,
list(r=sprintf("%.2f", c$estimate))),
adj=c(0, 1))
}
And it works perfectly!
Thanks a lot,
Xavier
Greg Snow wrote :
> The xy.coords function is a powerful function used by a lot of the plotting functions because it allows the user to enter x and y coordinates as 2 vectors, a 2 column matrix, or a list with x and y components (and possibly others), but it does not do 'topleft' and the like. The legend function (and some others) have special handling to recognize and implement strings like 'topleft'. One of the easier ways to plot something in the top left (or other relative positions) is to include the following line in your panel function:
>
> usr <- par('usr')
>
> Now the vector user will have 4 values: xleft, xright, ybottom, ytop. To place text in the topleft corner, just do:
>
> text(usr[1], usr[4], 'your text here', adj=c(0,1) )
>
> or something like:
>
> text( usr[1] + (usr[2]-usr[1])/10, usr[4] - (usr[4]-usr[3])/10, 'your text' )
>
> to center the text 10% of the way in from the topleft corner. The other corners and sides should be fairly easy to work out from the above.
>
> Other options include using the grconvertX and grconvertY functions.
>
> Hope this helps,
>
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> 801.408.8111
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Xavier Robin
>> Sent: Thursday, October 23, 2008 5:58 AM
>> To: r-help at r-project.org
>> Subject: [R] xy.coords in text
>>
>> Hello,
>>
>> 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",
>> c$estimate))))
>> }
>>
>> 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?
>>
>> Thanks,
>> Xavier
>>
>> --
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
--
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