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

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Thu Nov 29 17:34:21 CET 2007


Gavin Simpson wrote:
> 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?
>   
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
> 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))
>
>   


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-devel mailing list