[R] Why does plot() ignore the data type for axis labels?

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Feb 20 00:54:19 CET 2008


On Tue, 2008-02-19 at 14:40 -0800, Stiffler wrote:
> 
> 
> Mark Difford wrote:
> > 
> >>> I was wondering why the plot() command ignores the datatype when
> >>> displaying axis labels...
> > 
> > plot() doesn't ignore the datatype:
> > [...]
> > plot(x,y) calls xy.coords(), which recasts x as: x = as.double(x), which
> > is fine, since x is (also/primarily) numeric.

Mark,

I think this is a red herring. The issue is in drawing the axes and how
the ticks are chosen and labeled, not how the points are drawn on the
plotting region and whether they are recast as doubles. For all intents
and purposes, the casting as double hasn't changed the data at all:

> all.equal(x, as.double(x))
[1] TRUE
> x == as.double(x)
[1] TRUE TRUE TRUE

The issue appears to be that Axis.default ends up getting called because
there isn't an Axis method for class "integer". In amongst all that,
'pretty' ticks and labels are chosen as per ?pretty

> pretty(x) # x is integer still yet we get non-integer ticks
[1] 1.0 1.5 2.0 2.5 3.0

Herein lies the problem.

> > 
> > HTH, Mark.
> > 
> > 

Stiffler,

> Thanks for the explanation Mark.
> 
> If integers are being recast as doubles R is ignoring/overriding the user's
> choice of data-type. There may be good reasons for doing that internally
> (uniformity, code re-use etc) , but it is not what I'd expect as an end-user
> --- neither ?plot nor ?xy.coords seem to mention that coordinates need to be
> floating point numbers.

They don't need to be! It is just a tick number/location issue and
labelling problem - nothing sinister going on. In effect R is treating
the data as if they come from a continuous variable because it doesn't
know what to do with integers as a special case. As shown above,
recasting as doubles doesn't change the data, or the positions of the
points.

If the axes aren't to your liking using the defaults, then turn them off
and draw your own, as per my earlier reply.

Why doesn't R respect user's choice of data-type in this instance? I
guess the DIY solution is so simple that no-one on R Core or the wider R
community thought it worth writing, contributing and maintaining code
for handling integers as a special case.

HTH

G

> 
> Best regards,
> Stiffler
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list