Bug 10472 - Lack of 'table' method with points/lines
Lack of 'table' method with points/lines
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Wishlist
old
All Mac OS X v10.5
: P5 normal
Assigned To: Jitterbug compatibility account
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2007-11-27 22:23 UTC by Jitterbug compatibility account
Modified: 2010-05-22 20:35 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jitterbug compatibility account 2007-11-27 22:23:00 UTC
From: carlos@necsi.org
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


Comment 1 Jitterbug compatibility account 2007-11-29 21:36:03 UTC
From: Peter Dalgaard <P.Dalgaard@biostat.ku.dk>
carlos@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.)
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>   


-- 
   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@biostat.ku.dk)                  FAX: (+45) 35327907


Comment 2 Jitterbug compatibility account 2007-11-29 22:03:48 UTC
From: Gavin Simpson <gavin.simpson@ucl.ac.uk>
On Thu, 2007-11-29 at 16:36 +0100, Peter Dalgaard wrote:
> carlos@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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

Comment 3 Jitterbug compatibility account 2007-11-29 22:34:21 UTC
From: Peter Dalgaard <P.Dalgaard@biostat.ku.dk>
Gavin Simpson wrote:
> On Thu, 2007-11-29 at 16:36 +0100, Peter Dalgaard wrote:
>   
>> carlos@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@biostat.ku.dk)                  FAX: (+45) 35327907


Comment 4 Jitterbug compatibility account 2007-11-29 23:19:29 UTC
From: Gavin Simpson <gavin.simpson@ucl.ac.uk>
On Thu, 2007-11-29 at 17:34 +0100, Peter Dalgaard wrote:
> Gavin Simpson wrote:
> > On Thu, 2007-11-29 at 16:36 +0100, Peter Dalgaard wrote:
> >   
> >> carlos@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

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

> > 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@biostat.ku.dk)                  FAX: (+45) 35327907
> 
> 
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

Comment 5 Jitterbug compatibility account 2007-11-30 15:24:29 UTC
From: Peter Dalgaard <P.Dalgaard@biostat.ku.dk>
Gavin Simpson wrote:
> On Thu, 2007-11-29 at 17:34 +0100, Peter Dalgaard wrote:
>   
>> Gavin Simpson wrote:
>>     
>>> On Thu, 2007-11-29 at 16:36 +0100, Peter Dalgaard wrote:
>>>   
>>>       
>>>> carlos@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
>>     
>
> 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?
>   
It's been moved there already by someone ( who also attached a comment
which may sound harsher than meant...)

    -p
> All the best,
>
> G
>
>   
>>> 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@biostat.ku.dk)                  FAX: (+45) 35327907
>>
>>
>>     


-- 
   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@biostat.ku.dk)                  FAX: (+45) 35327907


Comment 6 Jitterbug compatibility account 2007-11-30 15:31:00 UTC
NOTES:
 There is no 'table' method for points/lines
Comment 7 Jitterbug compatibility account 2007-11-30 16:31:11 UTC
Audit (from Jitterbug):
Wed Nov 28 10:44:04 2007	ripley	changed notes
Wed Nov 28 09:44:04 2007	ripley	moved from incoming to trashcan
Fri Nov 30 08:31:39 2007	ripley	moved from trashcan to wishlist
Fri Nov 30 10:31:11 2007	ripley	changed notes
Comment 8 Brian Ripley 2010-05-22 20:35:39 UTC
added for 2.12.0