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
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.
Sender: r-help-bounces at r-project.org On-Behalf-Of: Greg.Snow at imail.org Subject: Re: [R] xy.coords in text Message-Id: <B37C0A15B8FB3C468B5BC7EBC7DA14CC6158B3A29B at LP-EXMBVS10.CO.IHC.COM> Recipient: ngottlieb at marinercapital.com -------------------------------------------------------- This information is being sent at the recipient's request or with their specific understanding. The recipient acknowledges that by sending this information via electronic means, there is no absolute assurance that the information will be free from third party access, use, or further dissemination. This e-mail contains information that is privileged and/or confidential and may be subject to legal restrictions and penalties regarding its unauthorized disclosure or other use. You are prohibited from copying, distributing or otherwise using this information if you are not the intended recipient. Past performance is not necessarily indicative of future results. This is not an offer of or the solicitation for any security which will be made only by private placement memorandum that may be obtained from the applicable hedge fund. If you have received this e-mail in error, please notify us immediately by return e-mail and delete this e-mail and all attachments from your system. Thank You. -------------- next part -------------- An embedded message was scrubbed... From: Greg Snow <Greg.Snow at imail.org> Subject: Re: [R] xy.coords in text Date: Thu, 23 Oct 2008 12:37:05 -0600 Size: 7811 URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081023/8a7666a2/attachment-0002.mht>
Sender: r-help-bounces at r-project.org On-Behalf-Of: Greg.Snow at imail.org Subject: Re: [R] xy.coords in text Message-Id: <B37C0A15B8FB3C468B5BC7EBC7DA14CC6158B3A29B at LP-EXMBVS10.CO.IHC.COM> Recipient: nalbicelli at tricadiacdpcmanagement.com -------------------------------------------------------- This information is being sent at the recipient's request or with their specific understanding. The recipient acknowledges that by sending this information via electronic means, there is no absolute assurance that the information will be free from third party access, use, or further dissemination. This e-mail contains information that is privileged and/or confidential and may be subject to legal restrictions and penalties regarding its unauthorized disclosure or other use. You are prohibited from copying, distributing or otherwise using this information if you are not the intended recipient. Past performance is not necessarily indicative of future results. This is not an offer of or the solicitation for any security which will be made only by private placement memorandum that may be obtained from the applicable hedge fund. If you have received this e-mail in error, please notify us immediately by return e-mail and delete this e-mail and all attachments from your system. Thank You. -------------- next part -------------- An embedded message was scrubbed... From: Greg Snow <Greg.Snow at imail.org> Subject: Re: [R] xy.coords in text Date: Thu, 23 Oct 2008 12:37:05 -0600 Size: 7811 URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081023/479f62d1/attachment-0003.mht>