Joshua Wiley
2011-Feb-01 19:00 UTC
[Rd] dotchart {graphics} 2.11.1 vs. 2.12.1 [followed up from Rhelp]
Dear List, With the R 2.12.0 addition of table methods for points(), dotchart() struggles with tables. I found several possible solutions, but it is beyond my skill to decide what is "best". Here is a small example: ############################################# x <- table(infert$education) y <- 1:3L dotchart(x) # error about incorrect plot type ## moving closer to the cause, dotchart() essentially does plot(x = c(0, 150), y = c(0, 5), type = "n") points(x, y) # error, because arg #2 is the type ## One option would be to explicitly name the arguments in dotchart() points(x = x, y = y) # "works", but misses the point (no pun intended) ## Another possibility would be to add unclass(). I'm not sure ## if this would have other repercussions. line 71 could become: points(unclass(x), y, pch = pch, col = color, bg = bg) ## Other options could be adding a method for dotchart() ## or to update the table method for points() points.table <- function (x, y, type = "h", lwd = 2, ...) { if (length(dim(x)) == 1L) { nx <- dimnames(x)[[1L]] is.num <- suppressWarnings(!any(is.na(xx <- as.numeric(nx)))) x0 <- if (is.num) xx else seq.int(x) if (missing(y)) { points(x0, unclass(x), type = type, lwd = lwd, ...) } else points(unclass(x), y, type = type, lwd = lwd, ...) } else stop("only for 1-D table") } plot(x = c(0, 150), y = c(0, 5), type = "n") points(x, y, type = "p") ############################################# -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Prof Brian Ripley
2011-Feb-18 13:44 UTC
[Rd] dotchart {graphics} 2.11.1 vs. 2.12.1 [followed up from Rhelp]
On Tue, 1 Feb 2011, Joshua Wiley wrote:> Dear List, > > With the R 2.12.0 addition of table methods for points(), dotchart() > struggles with tables.Which of course it is not documented to accept, not even the base type of such tables, a 1-D array. So this is detecting a previously undetected user error. Because the table methods of lines/points have different defaults, your suggestions are not going to work (did you actually try them with the dotchart example?). The best we can do is to throw an error if 'y' is specified in a table method.> I found several possible solutions, but it is beyond my skill to > decide what is "best". Here is a small example:dotchart(as.vector(x)) works, but is probably not what you want. dotchart(unclass(x)) works, but it is not guaranteed to keep working. Unclassing is definitely dangerous, since the test is.numeric(x) invokes a generic function. If methods have been written following the help page (and not all have), as.numeric(x) should do the trick, but needs to be done after names are extracted. OTOH, learning the discipline of consulting the help pages if things do not work as you expect is a good one, since there is no way R is going to check all inputs to all functions.> ############################################# > x <- table(infert$education) > y <- 1:3L > > dotchart(x) # error about incorrect plot type > > ## moving closer to the cause, dotchart() essentially does > plot(x = c(0, 150), y = c(0, 5), type = "n") > points(x, y) # error, because arg #2 is the type > > ## One option would be to explicitly name the arguments in dotchart() > points(x = x, y = y) # "works", but misses the point (no pun intended) > > ## Another possibility would be to add unclass(). I'm not sure > ## if this would have other repercussions. line 71 could become: > points(unclass(x), y, pch = pch, col = color, bg = bg) > > ## Other options could be adding a method for dotchart() > ## or to update the table method for points() > points.table <- function (x, y, type = "h", lwd = 2, ...) > { > if (length(dim(x)) == 1L) { > nx <- dimnames(x)[[1L]] > is.num <- suppressWarnings(!any(is.na(xx <- as.numeric(nx)))) > x0 <- if (is.num) > xx > else seq.int(x) > if (missing(y)) { > points(x0, unclass(x), type = type, lwd = lwd, ...) > } else points(unclass(x), y, type = type, lwd = lwd, ...) > } > else stop("only for 1-D table") > } > > plot(x = c(0, 150), y = c(0, 5), type = "n") > points(x, y, type = "p") > > ############################################# > > > > -- > Joshua Wiley > Ph.D. Student, Health Psychology > University of California, Los Angeles > http://www.joshuawiley.com/-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595