[R] graphical behavior of a table of numbers

Richard M. Heiberger rmh at temple.edu
Mon Jan 30 21:04:23 CET 2017


I still think

plot(fr, xlab="Determinant", ylab="Frequency")

has a totally non-intuitive x-axis.

I recommend that the example in R-intro.pdf include an additional
sentence and option.


## In this example, where the x-axis is the entire set of integers -81:81,
## displaying them as an ordinary numeric axis might be preferable, in
which case use
   plot(fr, xlab="Determinant", ylab="Frequency", xaxt="n")
   axis(1)


While thinking on this, I looked at ?plot.table
The example
     plot(table(state.division))
doesn't display most of the labels, and I think it therefore not a good example.
I recommend revising it, perhaps to
    old.oma <- par(oma=c(6,1,0,1));
    plot(table(state.division), las=2, mgp=c(.5,2,0))
    par(old.oma)
I agree the code for the legible labels is difficult to read.

Rich

On Mon, Jan 30, 2017 at 10:59 AM, Martin Maechler
<maechler at stat.math.ethz.ch> wrote:
>>>>>> Richard M Heiberger <rmh at temple.edu>
>>>>>>     on Mon, 30 Jan 2017 10:19:53 -0500 writes:
>
>     > Duncan, thank you for locating the problem.
>     > Martin, thank you for explaining the behavior and for the first pass
>     > at fixing it.
>
>
>     > With the fix, now the x-axis has ticks at all integers, and tick labels at
>     > c(-81,-67,-53,-39,-25,-11,0,9,19,31,43,55,67,79)
>
> Note that *which* tick labels are shown depends quite a bit
> on your graphics window width, your font sizes etc !
>
>     > This is with R-3.3.2, as I interpret your fix to be to only the
>     > R-intro.pdf manual with no change
>     > to the code of any of the functions.
>
> That's correct.  If this is about improving any of
> the base graphics functions,  there's the  R-devel mailing list
> and the bugzilla repository for "wishes"... rather than the
> R-help list.
>
>     > More work has to be done to repair the example.
>
> I strongly disagree:
> The example (which does not even need a 'type = "h"', and no longer
> uses it) now mentions that the plot.factor method is used.
> ... and I do like its graphics output, given the simplicity of
> the plot() function call.
>
> Code as the one below may be preferable in some cases, but not
> there in the   "Introduction to R".
>
>
>     > I recommend
>     > plot(as.numeric(fr) ~ as.numeric(names(fr)), type="h",
>     > xlab="Determinant", ylab="Frequency")
>
>     > The slightly more obvious solution doesn't work
>     >> plot(fr ~ as.numeric(names(fr)), type="h", xlab="Determinant", ylab="Frequency")
>     > Error in plot.table(c(-81, -80, -79, -78, -77, -76, -75, -74, -73, -72,  :
>     > invalid table 'x'
>
>     > ## It is possible to change graphics:::Axis.table to
>     > if (is.num) axis(side, ...)
>     > ## and that would make the x-axis for the determinant example
>     > plot(fr, type="h", xlab="Determinant", ylab="Frequency")
>     > ## look sensible, but would
>     > ## be less appropriate for the following example.
>
>     > ## The current behavior of Axis.table makes sense in this example
>     > tt <- as.table(array(c(10,20,30), dimnames=list(c(100, 120, 200))))
>     > tt
>     > plot(tt)
>
> Indeed.  I doubt we would want to change Axis.table()
> just because of examples like the determinant one....
> (and then again: such considerations would be part of a new thread on R-devel...)
>
> Martin Maechler
>
>
>     > On Mon, Jan 30, 2017 at 5:38 AM, Martin Maechler
>     > <maechler at stat.math.ethz.ch> wrote:
>     >>>>>>> Duncan Murdoch <murdoch.duncan at gmail.com>
>     >>>>>>> on Sun, 29 Jan 2017 06:32:27 -0500 writes:
>     >>
>     >> > On 29/01/2017 12:05 AM, Jim Lemon wrote:
>     >> >> Hi Richard, I think there may be something amiss in the
>     >> >> plot.table function. As you note, changing the class of
>     >> >> fr to array produces a more sensible plot, as does Bert's
>     >> >> "as.vector". Yet inside plot.table we find:
>     >> >>
>     >> >> plot(x0, unclass(x), ...
>     >> >>
>     >> >> and that should produce an array:
>     >> >>
>     >> >> class(unclass(fr)) [1] "array"
>     >> >>
>     >> >> The plot.table function looks like it should produce the
>     >> >> plot you want, but it doesn't. I think (therefore I am
>     >> >> probably wrong) that a 1D table is handled in the same
>     >> >> way as multiD table rather than being squeezed into a
>     >> >> vector.
>     >>
>     >> > I think the issue is that Axis() is called without
>     >> > removing the class.  Axis.table sets ticks based on the
>     >> > names of the table.
>     >>
>     >> > Duncan Murdoch
>     >>
>     >> yes indeed!  So this answers Rich Heiberger's question.
>     >>
>     >> The example stems from a time long before there was
>     >> a plot.table() method, and even longer before plot.default() had
>     >> started using  Axis() and its methods.
>     >>
>     >> So a much nicer example for the R-intro -- committed a few
>     >> minutes ago -- is making use of the  plot.table() S3 method :
>     >>
>     >> d <- outer(0:9, 0:9)
>     >> fr <- table(outer(d, d, "-"))
>     >> plot(fr, type="h", xlab="Determinant", ylab="Frequency")
>     >>
>     >> So this fulfills Rich's recommendation.
>     >>
>     >> Martin
>     >>
>     >>
>     >> >> On Sun, Jan 29, 2017 at 11:19 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
>     >> >>> Rich:
>     >> >>>
>     >> >>> Simpler: Just lose the "table" class.
>     >> >>>
>     >> >>> plot(as.numeric(names(fr)), as.vector(fr),  type="h",
>     >> >>>      xlab="Determinant", ylab="Frequency")
>     >> >>>
>     >> >>> However, I'm no less puzzled by the "strange" behavior than you.
>     >> >>>
>     >> >>> In addition, it's probably worth noting that xyplot in lattice (and no
>     >> >>> doubt ggplot,too) does not have this problem (as I'm sure you know):
>     >> >>>
>     >> >>> xyplot(fr ~ as.numeric(names(fr)),  type="h",
>     >> >>>        xlab="Determinant", ylab="Frequency")
>     >> >>>
>     >> >>>
>     >> >>> Cheers,
>     >> >>> Bert
>     >> >>> Bert Gunter
>     >> >>>
>     >> >>> "The trouble with having an open mind is that people keep coming along
>     >> >>> and sticking things into it."
>     >> >>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>     >> >>>
>     >> >>>
>     >> >>> On Sat, Jan 28, 2017 at 3:03 PM, Richard M. Heiberger <rmh at temple.edu> wrote:
>     >> >>>> ## This example is from R-intro.pdf page 21 (R-3.3.2)
>     >> >>>>
>     >> >>>> d <- outer(0:9, 0:9)
>     >> >>>> fr <- table(outer(d, d, "-"))
>     >> >>>> plot(as.numeric(names(fr)), fr, type="h",
>     >> >>>>      xlab="Determinant", ylab="Frequency")
>     >> >>>> ## The y-axis tick marks are at c(-21,24,65).
>     >> >>>> ## This seems to be because class(fr) == "table"
>     >> >>>>
>     >> >>>> ## Switching the class to array gives the more appropriate
>     >> >>>> ## y-axis ticks at seq(0,500,100) .
>     >> >>>>
>     >> >>>> fr.array <- fr
>     >> >>>> class(fr.array) <- "array"
>     >> >>>> plot(as.numeric(names(fr)), fr.array, type="h",
>     >> >>>>      xlab="Determinant", ylab="Frequency")
>     >> >>>>
>     >> >>>>
>     >> >>>> ## I have a question and a recommendation.
>     >> >>>> ## Question:
>     >> >>>> ## Why are the y-axis ticks for the table defaulted to c(-21,24,65).
>     >> >>>> ##
>     >> >>>> ## Recommendation:
>     >> >>>> ## Changed the example on page 21 to show the ticks at seq(0,500,100)?
>     >> >>>>
>     >> >>>> ## Rich
>     >> >>>>
>     >>
>     >> ______________________________________________
>     >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>     >> 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.



More information about the R-help mailing list