[R] [Fwd: Re: Plotting log-axis with the exponential base to
(Ted Harding)
Ted.Harding at manchester.ac.uk
Tue May 11 12:52:37 CEST 2010
Elisabeth and I have been corresponding off-list about this, and
came to a potential solution which is on the lines also outlined
by Mark Difford.
Where Elisabeth (rather, her tutor) may have become confused may
lie in the fact that, with a simple plot(...,log="y"), R will
(by default) make its own decision about what numbers (in the raw
scale) to put on the Y-axis as annotations. These will be "nice"
(or, in R-doc-language, "pretty") numbers favouring simple multiples
and submultiples of powers of 10. That may be why the plot gave the
impression of being ""a logaritmic axis with the base of 10".
The solution, as Elisabeth and I (and later Mark) came to was to
suppress the Y-axis in the first instance when using plot(),
thus plot(...,log="y",yaxt="n"). Then you add the annotation
you want ("custom Y-axis") using the axis() function. The example
we came to as paradigm was:
set.seed(54321)
Y <- 70*runif(100)
pwrs2<-2^((floor(log2(min(Y))):ceiling(log2(max(Y)))))
##[1] 0.5 1.0 2.0 4.0 8.0 16.0 32.0 64.0 128.0
##as.character(pwrs2) =
##[1] "0.5" "1" "2" "4" "8" "16" "32" "64" "128"
plot(Y,log="y",yaxt="n",ylim=c(min(pwrs2),max(pwrs2)))
axis(side=2,at=pwrs2,labels=as.character(pwrs2),las=1)
It is looking as though this will be the basis for a successful
solution in Elisabeth's real application.
However, there is another little "trap" lurking in there, best
illustrated by Mark's dataset:
plot((1:10), log="y", yaxt="n")
axis(side=2, at=c(1,2,5,10))
Here the data are X=(1:10), Y=(1:10), i.e. a straight line Y=X
in the raw (X,Y) plane. No purer candidate for a regression line
could be imagined. So let us try to add the regression to the plot.
Since it joins (0,0) to (10,10), let's try (after the above plot
commands):
lines(c(1,10),c(1,10))
Well, this has taken the points (1,1) and (10,10) on the plot,
with the Y-axis duly scaled logarithmically, and joined them.
But what it has joined them with is a straight line on the
logarithmic plot itself. I.e. it has not computed intermediate
points on a logarithmic scale. Therefore, as a logarithmic
representation of the straight-line regression Y=X, it is false!
One solution is to construct it explicitly over the intermediate
points:
lines(0.1*(10:100),0.1*(10:100))
so that now each intermediate point has its Y-coordinate log
transformed, and the straight-line segments on the graph will
now approximate to the logarithmic curve that one wanted in the
first place.
I don't know of another way to do this: for instance, log="y" will
not work with lines(), since '"log" is not a graphical parameter'.
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 11-May-10 Time: 11:52:30
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list