Skip to content

Problem with points/lines (PR#10472)

6 messages · Carlos Gershenson, Peter Dalgaard, Gavin Simpson

#
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
1 day later
#
carlos at necsi.org wrote:
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.)

  
    
#
On Thu, 2007-11-29 at 16:36 +0100, Peter Dalgaard wrote:
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))
#
Gavin Simpson wrote:
As always, there is a risk that it falls through the cracks before
someone gets around to looking at it.

Your code look sane though, and as far as I can see, you do not even
have to do much in terms of documentation since it could be wedged into
the existing plot.table() help page.

For now, we could file under "wishlist". As you know, items on the
wishlist do in fact get fulfilled sometimes.

    -p

  
    
#
On Thu, 2007-11-29 at 17:34 +0100, Peter Dalgaard wrote:
OK. I'll prepare a patch against the R development SVN for the Rd file
and take another look at the code I wrote. Can you refile this bug under
"wishlist" or do I need to submit another bug report to achieve this?

All the best,

G
#
Gavin Simpson wrote:
It's been moved there already by someone ( who also attached a comment
which may sound harsher than meant...)

    -p