round function in geom_text

2,935 views
Skip to first unread message

pradip...@samhsa.hhs.gov

unread,
Sep 19, 2012, 5:02:26 PM9/19/12
to ggp...@googlegroups.com, pradip...@samhsa.hhs.gov
 

Hello,

 

I am using the round function (as shown below) for gem_text.  In the ggplot2 chart, the text is printed as 24 for "round (24.0,1)".  Could anyone please let me know what I am doing wrong here?

 

####################

geom_text (aes(label=round(est,1)), position=position_dodge(width=0.9),

################

 

I have tested the code in a separate R session. I don't understand why the round function of the same number (in this case it is 24.0) give somewhat different results depending on the length of the vector (1-element vs. 2-element vector)? Please see below.

 

############

 x <- print (round(24.0,1))

[1] 24

 

 y <- print (round (c(24.0, 24.1),1))

[1] 24.0 24.1

#############

 

Thanks,

 

Pradip Muhuri

Ista Zahn

unread,
Sep 19, 2012, 9:09:57 PM9/19/12
to pradip...@samhsa.hhs.gov, ggp...@googlegroups.com
Hi,

I don't think this has anything to do with round()

> print(24)
[1] 24
> print(24.1)
[1] 24.1
> print(c(24.0, 24.1))
[1] 24.0 24.1

So it doesn't have anything to to with round(), or with the length of x.

If you want to want to keep trailing zeros I think you will have to
turn to something like formatC or sprintf.

> formatC(c(24.0, 24.1), format="f", digits=1)
[1] "24.0" "24.1"

Best,
Ista
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Sep 20, 2012, 12:23:12 PM9/20/12
to Ista Zahn, ggp...@googlegroups.com
Ista,

Thank you so much. The FormatC function worked fine for trailing zeros.

Pradip

Pradip K. Muhuri, PhD
Statistician
Substance Abuse & Mental Health Services Administration
The Center for Behavioral Health Statistics and Quality
Division of Population Surveys
1 Choke Cherry Road, Room 2-1071
Rockville, MD 20857
 
Tel: 240-276-1070
Fax: 240-276-1260
e-mail: Pradip...@samhsa.hhs.gov
 
The Center for Behavioral Health Statistics and Quality your feedback.  Please click on the following link to complete a brief customer survey:  http://cbhsqsurvey.samhsa.gov
Reply all
Reply to author
Forward
0 new messages