[R] scales percent precision
Jacob Wegelin
jacobwegelin at fastmail.fm
Thu Feb 27 20:55:05 CET 2014
Incidentally,
?scales::percent
brings up exactly the same text as
?scales::percent_format
On 2014-02-27 Thu 14:47, Jacob Wegelin wrote:
> But percent_format() does not take the argument, multiply it by 100, and
> paste on a percent sign, as we see here:
>
>> ?scales::percent_format
>> percent_format(0.0101010101)
> Error in percent_format(0.0101010101) : unused argument(s) (0.0101010101)
>> args(percent_format)
> function () NULL
>
> And how do we control the significant digits when we use percent()?
>
>> percent(0.0101010101)
> [1] "1.01%"
>
> My point is that
>
>> ?scales::percent_format
>
> does not answer these questions. This is what I mean by saying that the
> function is not documented.
>
> On 2014-02-27 Thu 14:34, Dennis Murphy wrote:
>> Hi:
>>
>> On Thu, Feb 27, 2014 at 8:49 AM, Jacob Wegelin <jacobwegelin at fastmail.fm>
>> wrote:
>>>
>>> scales::percent appears not to be documented.
>>
>> ?scales::percent_format
>>
>> where it tells you that it takes its argument, multiplies it by 100
>> and then attaches a percent sign to it. For most situations, the data
>> should be relative frequencies/proportions. BTW, many of the functions
>> in the scales package are second-order R functions, which means there
>> are two calls in the function invocation. The first call returns a
>> function and the second is a call to the returned function.
>>
>>>
>>> Details:
>>>
>>> At http://cran.r-project.org/web/packages/scales/scales.pdf, equivalently
>>> in
>>> ?percent, I find no answer to the following two questions.
>>>
>>> (1) How can I specify the number of decimal points in a call to percent()?
>>> For instance, 0.010101 could be
>>>
>>> 1%
>>>
>>> 1.0%
>>>
>>> 1.01%
>>>
>>> etc. depending on what kind of report I'm writing.
>>>
>>> I can control precision myself by writing
>>>
>>> mypercent<-function(theargument, siglevel=2) {
>>> stopifnot(is.numeric(theargument))
>>> paste(signif(theargument, siglevel) * 100, "%", sep="")
>>> }
>>>
>>> and then we have
>>>
>>>> mypercent(0.010101)
>>>
>>> [1] "1%"
>>>>
>>>> mypercent(0.010101, 5)
>>>
>>> [1] "1.0101%"
>>>>
>>>> mypercent(0.010101, 3)
>>>
>>> [1] "1.01%"
>>
>> percent_format() uses pretty breaks by default, so you'd probably want
>> to pass your desired labels to scale_y_continuous() directly and avoid
>> percent_format(). You could call the function on a vector of breaks
>> and use the return values for the labels.
>>
>>>
>>> (2) What is the function precision() inside percent()? I find no
>>> documentation for it, and in fact it does not appear in the search path.
>>> Nor
>>> does round_any().
>>
>> round_any() comes from the plyr package. I have no idea where
>> precision() comes from; I've wondered about that myself a couple of
>> times. I imagine it comes from one of the imported packages, but I
>> didn't find it in any of plyr, stringr or labeling. I didn't check the
>> color-related packages (RColorBrewer, dichromat or munsell). It could
>> also be a hidden function.
>>
>> Dennis
>>>
>>>> percent(0.010101)
>>>
>>> [1] "1.01%"
>>>>
>>>> percent
>>>
>>> function (x) {
>>> x <- round_any(x, precision(x)/100)
>>> str_c(comma(x * 100), "%")
>>> }
>>> <environment: 0x10c0f9350>
>>>>
>>>> find("precision")
>>>
>>> character(0)
>>>>
>>>> find("round_any")
>>>
>>> character(0)
>>>>
>>>>
>>>
>>> Thanks for any insights
>>>
>>> Jacob Wegelin
>>>
>>>> sessionInfo()
>>>
>>> R version 2.15.3 (2013-03-01)
>>> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
>>>
>>> locale:
>>> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>>>
>>> attached base packages:
>>> [1] tools grid splines stats graphics grDevices utils
>>> datasets methods base
>>>
>>> other attached packages:
>>> [1] scales_0.2.3 xtable_1.7-0 reshape2_1.2.2 moments_0.13
>>> corrplot_0.70 ggplot2_0.9.3.1 nlme_3.1-108
>>>
>>> loaded via a namespace (and not attached):
>>> [1] colorspace_1.2-0 dichromat_1.2-4 digest_0.6.0 gtable_0.1.2
>>> labeling_0.1 lattice_0.20-13 MASS_7.3-23 munsell_0.4
>>> [9] plyr_1.8 proto_0.3-10 psych_1.2.8
>>> RColorBrewer_1.0-5 stringr_0.6.2
>>>>
>>>>
>>>
>>> ______________________________________________
>>> 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.
>>
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list