[Rd] Problem with points/lines (PR#10472)

Gavin Simpson gavin.simpson at ucl.ac.uk
Thu Nov 29 17:03:48 CET 2007


On Thu, 2007-11-29 at 16:36 +0100, Peter Dalgaard wrote:
> carlos at necsi.org wrote:
> > Full_Name: Carlos Gershenson
> > Version: 2.6.1
> > OS: MacOSX
> > Submission from: (NULL) (24.128.48.138)
> >
> >
> > Hi,
> >
> > Try this out:
> >
> > x<-1:10
> > y<-x/2
> > plot(table(x))
> > points(table(y))
> > #or lines(table(y))
> >
> > No matter what's the value of y, it prints out in the coordinates of x... this
> > happens only with tables, not with simple plot(x), points(y), and table(y) works
> > fine
> >
> >   
> The real issue is that we have a plot method for tables, which tries to
> be smart about using numerical entry names. There's no similar points
> method, nor a lines method, so in those cases you get the default
> method, namely to plot the table values (all ones) against the *index*,
> i.e.,1:n. This shows the effect quite clearly:
> 
> plot(table(x^2)
> lines(table(x))
> 
> (This is not a bug, since noone has promised you that lines and point
> methods should exist. It could be taken as an enhancement request.)

Peter,

Re: your final statement above, would an enhancement request be looked
upon favourably by R Core for inclusion in R if some code and
documentation were supplied?

I replied earlier in the week to Carlos' query on R-Help (seems this has
taken 2 days to get the R-Devel?) with some quickly knocked together
code for points.table and Axis.table methods that were based in large
part on the code in plot.table. I reproduce these below. They were based
on printing the source plot.table at the prompt, not from interrogation
of the SVN version.

If considered for inclusion, I'd be happy to get these in better shape
and write appropriate Rd files as required?

All the best,

G

## points and Axis methods for objects of class "table"
## Gavin Simpson 2007 based in large part on plot.table
`points.table` <- function (x, type = "h", ...)
{
     rnk <- length(dim(x))
     if (rnk == 0)
         stop("invalid table 'x'")
     if (rnk == 1) {
         nx <- dimnames(x)[[1]]
         ow <- options(warn = -1)
         is.num <- !any(is.na(xx <- as.numeric(nx)))
         options(ow)
         x0 <- if (is.num)
             xx
         else seq.int(x)
         points(x0, unclass(x), type = type, ...)
     }
     else stop("only for 1-D table")
}

`Axis.table` <- function(x, at, ..., labels)
{
     rnk <- length(dim(x))
     if (rnk == 0)
         stop("invalid table 'x'")
     if (rnk == 1) {
         nx <- dimnames(x)[[1]]
         ow <- options(warn = -1)
         is.num <- !any(is.na(xx <- as.numeric(nx)))
         options(ow)
         x0 <- if (is.num)
             xx
         else seq.int(x)
         if(missing(at))
             at <- x0
         if(missing(labels))
             labels <- nx
         xaxt <- if (length(as <- list(...))) {
             if (!is.null(as$axes) && !as$axes)
                 "n"
             else as$xaxt
         }
         axis(1, at = at, labels = labels, xaxt = xaxt)
     }
     else stop("only for 1-D table")
}

## example to run with:
set.seed(1234)
x <- sample(1:10, 30, replace = TRUE)
y <- x / 2
plot(table(x), type = "p")
points(table(y), col = "red", type = "p", pch = 2)

## And if you need to redraw axes
## need to use Axis() as it is a generic version of axis()
plot(table(x), type = "p", axes = FALSE)
points(table(y), col = "red", type = "p", pch = 2)
Axis(table(y))

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 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-devel mailing list