Georg Ehret
2007-Nov-26 03:00 UTC
[R] determine the number of digits printed into graph with "text(...)"
Dear R comunity, I am printing a number in a graph using "text(...)" - see code below: "fraction" is my number. I wish to print only two digits of the number and struggle to do this... options(digit=3) does not work... Thank you for your suggestions! Cheers, Georg. ********************************** Georg Ehret Johns Hopkins Baltimore callrates<-read.table("RSG",col.names=c("variant","callrate")) hist(callrates[,2],breaks=100) fraction<-nrow(callrates[callrates$callrate>=0.9,])/nrow(callrates) hist(callrates[,2],breaks=100) text(0.4,500,"proportion of callrates >= 0.9:") text(0.4,450,fraction)
Peter Alspach
2007-Nov-26 03:22 UTC
[R] determine the number of digits printed into graph with"text(...)"
Georg ?format (or simply ?round if you are happy to drop the occasional final 0) HTH ....... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Georg Ehret > Sent: Monday, 26 November 2007 4:00 p.m. > To: r-help > Subject: [R] determine the number of digits printed into > graph with"text(...)" > > Dear R comunity, > I am printing a number in a graph using "text(...)" - see code > below: "fraction" is my number. I wish to print only two > digits of the number and struggle to do this... > options(digit=3) does not work... > Thank you for your suggestions! Cheers, Georg. > > ********************************** > Georg Ehret > Johns Hopkins > Baltimore > > callrates<-read.table("RSG",col.names=c("variant","callrate")) > hist(callrates[,2],breaks=100) > fraction<-nrow(callrates[callrates$callrate>=0.9,])/nrow(callrates) > hist(callrates[,2],breaks=100) > text(0.4,500,"proportion of callrates >= 0.9:") > text(0.4,450,fraction) > > ______________________________________________ > 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. >The contents of this e-mail are privileged and/or confidential to the named recipient and are not to be used by any other person and/or organisation. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail.
jim holtman
2007-Nov-26 03:37 UTC
[R] determine the number of digits printed into graph with "text(...)"
sprintf("%.2f", fraction) On Nov 25, 2007 10:00 PM, Georg Ehret <georgehret at gmail.com> wrote:> Dear R comunity, > I am printing a number in a graph using "text(...)" - see code > below: "fraction" is my number. I wish to print only two digits of the > number and struggle to do this... options(digit=3) does not work... > Thank you for your suggestions! Cheers, Georg. > > ********************************** > Georg Ehret > Johns Hopkins > Baltimore > > callrates<-read.table("RSG",col.names=c("variant","callrate")) > hist(callrates[,2],breaks=100) > fraction<-nrow(callrates[callrates$callrate>=0.9,])/nrow(callrates) > hist(callrates[,2],breaks=100) > text(0.4,500,"proportion of callrates >= 0.9:") > text(0.4,450,fraction) > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Prof Brian Ripley
2007-Nov-26 07:29 UTC
[R] determine the number of digits printed into graph with "text(...)"
Your misunderstanding is that this is not 'printing'. From ?text: labels: a character vector or expression specifying the _text_ to be written. An attempt is made to coerce other language objects (names and calls) to expressions, and vectors and other classed objects to character vectors by 'as.character'. If you don't want what as.character does (see its help page), you need to do the conversion to character yourself, e.g. by sprintf() or format(). On Sun, 25 Nov 2007, Georg Ehret wrote:> Dear R comunity, > I am printing a number in a graph using "text(...)" - see code > below: "fraction" is my number. I wish to print only two digits of the > number and struggle to do this... options(digit=3) does not work... > Thank you for your suggestions! Cheers, Georg. > > ********************************** > Georg Ehret > Johns Hopkins > Baltimore > > callrates<-read.table("RSG",col.names=c("variant","callrate")) > hist(callrates[,2],breaks=100) > fraction<-nrow(callrates[callrates$callrate>=0.9,])/nrow(callrates) > hist(callrates[,2],breaks=100) > text(0.4,500,"proportion of callrates >= 0.9:") > text(0.4,450,fraction)-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595