[R] problem with plotting table

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Nov 27 23:00:07 CET 2007


Duncan Murdoch wrote:
> Carlos Gershenson wrote:
>> Hi all,
>>
>> Let us have:
>>
>> x<-1:10
>> y<-x/2
>> plot(table(x), type="p")
>> points(table(y), pch=2)
>>
>>
>> Why does the last command plots the values of table(y) using the x  
>> coordinates of table(x)???
>>   
> 
> It's just a coincidence.  It's actually using the indices 1:10, 
> regardless of the values of x.
> 
> You've given points() a vector of 10 values to plot, and by default it 
> plots them against their index.
> 
> To get what you want you need
> 
> temp <- table(y)
> points(names(temp), temp)
> 
> The reason plot() worked is because it has a method for tables, but 
> points() does not.  In general the high level plot functions in R tend 
> to have lots of methods, but the low level ones make you do the work.

Which doesn't mean you can't write your own method for points so 'it 
just works'. For example, lifting the relevant bits from 
getAnywhere(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")
}

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 draw axes, following a similar format:

## need to use Axis() as it is a generic version of axis()
`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")
}

set.seed(1234)
x <- sample(1:10, 30, replace = TRUE)
y <- x / 2
plot(table(x), type = "p", axes = FALSE)
points(table(y), col = "red", type = "p", pch = 2)
Axis(table(y))

HTH

G

> 
> Duncan Murdoch
>> Am I doing something wrong?
>> What would be a way of plotting the points of table(y) on their place?
>>
>> #this problem also occurs with:
>> plot(table(y), type="p")
>> points(table(x), pch=2)
>>
>>
>> Thank you very much,
>> Carlos
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
  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