[R] plot representation of calculated value known to be 7.4

Bert Gunter bgunter.4567 at gmail.com
Tue Dec 26 16:35:39 CET 2017


Inline.

-- Bert

> Thanks a lot - formatting the ordinate as ylim=c(4,10) before plotting pH also removed the problem, and options(digits=10) confirmed that pH was not all exactly 7.4 - as I knew. Still I wonder just why R chooses to plot(ATOT,pH) as shown with repeated "7.4" instead of some more detailed representation. Thanks a gain and happy New Year!
--------------------
There is no need to wonder if you just follow the docs in detail:

Your first stop should be ?plot.default. If you read carefully through
it, you will find in the examples:

##--- Log-Log Plot  with  custom axes

In this example, you will find the use of the axis() function to
create the custom axes. So this suggests, to me anyway, that ?axis
should be the next port of call.

?axis reveals that axis() has a "labels" argument. The details section
of the docs say:

"If labels is not specified, the numeric values supplied or calculated
for at are converted to character strings as if they were a numeric
vector printed by print.default(digits = 7)."

So -- aha! -- that looks like the explanation you sought. And, indeed,
checking ?print.default, it says in "Details":

"The same number of decimal places is used throughout a vector. This
means that digits specifies the minimum number of significant digits
to be used, and that at least one entry will be encoded with that
minimum number. However, if all the encoded elements then have
trailing zeroes, the number of decimal places is reduced until at
least one element has a non-zero final digit."

So, yes, it's a bit of a "Long Day's Journey...," but if you expend
the effort to follow it all through carefully, especially for R's base
functionality (it gets much more chancy with user-contributed
packages, of course) it almost aways **is** all there.

Cheers,
Bert





>
> Troels
>
>
> Den 26-12-2017 kl. 01:03 skrev Bert Gunter:
>
> Note that ?all.equal clearly says that it tests for **approximate equality only** with tolerance "close to 1.5 e-8.
>
> So..
>
> > all.equal(z,pH, tol = 1e-15)
> [1] "Mean relative difference: 6.732527e-11"
>
> and
>
> > print(pH, digits =15)
> ## output omitted
>
> Shows you what's going on.
>
> Cheers,
> Bert
>
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
> On Mon, Dec 25, 2017 at 11:00 AM, Troels Ring <tring at gvdnet.dk> wrote:
>>
>> Dear friends - copy paste missed
>>
>> SID <- c() before the first loop - sorry
>>
>> BW Troels
>>
>>
>>
>> Den 25-12-2017 kl. 19:12 skrev Troels Ring:
>>>
>>>
>>> Dear friends - merry Christmas and thanks a lot for much help during the year!
>>>
>>> In the example below I fail to understand how the calculated value pH is represented in a simple plot - also included. The calculations are useful in practice and likely to be right in principle but I cannot see how this occurs: why a calculated value of 7.4 known as numeric is not simply plotted as such. It happened on Windows both 7 and 10 with R version 3.4.1.
>>>
>>> All best wishes
>>>
>>> Troels
>>>
>>> ff <- function(H,SID,ATOT,ka)  H + SID - kw/H - ka*ATOT/(H+ka)
>>>  ka <- 1e-7
>>>  kw <- 1e-14
>>>  ATOT <- seq(0,0.3,length=100)*1e-3
>>>
>>>  for (i in 1:length(ATOT))  {
>>>  SID[i] <- uniroot(ff,c(-1,1),tol=.Machine$double.eps,maxiter=100000,ka=ka,
>>> ATOT=ATOT[i], H = 10^-7.4)$root}
>>> ATOT
>>>
>>> #confirm pH 0 7.4
>>>
>>> H <- c()
>>>  for (i in 1:length(ATOT))  {
>>>  H[i] <- uniroot(ff,c(1e-19,1),tol=.Machine$double.eps,maxiter=100000,ka=ka,
>>> ATOT=ATOT[i], SID = SID[i])$root}
>>>
>>> (pH <- -log10(H))
>>> plot(pH)
>>> str(pH)
>>> # num [1:100] 7.4 7.4 7.4 7.4 7.4 ...
>>> z <- rep(7.4,length(ATOT))
>>> all.equal(z,pH)
>>> #TRUE
>>> points(z,col="red")
>>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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