Hello, I was wondering why the plot() command ignores the datatype when displaying axis labels. More specifically, if the data points are integers then the axis labels should intuitively also be integers, right?> x <- as.integer(c(1,2,3)) > y <-x > typeof(x)[1] "integer"> plot(x,y) >The axis labels are 1.0, 1.5, 2.0, 2.5, 3.0 but if the integer type were taken into account they would be 1, 2, 3. PS what's the right way to get integer labels? Z. -- View this message in context: http://www.nabble.com/Why-does-plot%28%29-ignore-the-data-type-for-axis-labels--tp15562325p15562325.html Sent from the R help mailing list archive at Nabble.com.
Bert Gunter
2008-Feb-19 17:51 UTC
[R] Why does plot() ignore the data type for axis labels?
I don't do "why" answers. Only how. Occasionally. ?plot.default ##with the axes=FALSE argument. Then ?axis ## note the labels and at arguments. Bert Gunter Genentech Nonclinical Statistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Stiffler Sent: Tuesday, February 19, 2008 9:18 AM To: r-help at r-project.org Subject: [R] Why does plot() ignore the data type for axis labels? Hello, I was wondering why the plot() command ignores the datatype when displaying axis labels. More specifically, if the data points are integers then the axis labels should intuitively also be integers, right?> x <- as.integer(c(1,2,3)) > y <-x > typeof(x)[1] "integer"> plot(x,y) >The axis labels are 1.0, 1.5, 2.0, 2.5, 3.0 but if the integer type were taken into account they would be 1, 2, 3. PS what's the right way to get integer labels? Z. -- View this message in context: http://www.nabble.com/Why-does-plot%28%29-ignore-the-data-type-for-axis-labe ls--tp15562325p15562325.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Gavin Simpson
2008-Feb-19 18:11 UTC
[R] Why does plot() ignore the data type for axis labels?
On Tue, 2008-02-19 at 09:17 -0800, Stiffler wrote:> Hello, > > I was wondering why the plot() command ignores the datatype when displaying > axis labels. More specifically, if the data points are integers then the > axis labels should intuitively also be integers, right? > > > x <- as.integer(c(1,2,3)) > > y <-x > > typeof(x) > [1] "integer" > > plot(x,y) > > > > The axis labels are 1.0, 1.5, 2.0, 2.5, 3.0 but if the integer type were > taken into account they would be 1, 2, 3.It is due to pretty() finding nice numbers for the axis> pretty(x)[1] 1.0 1.5 2.0 2.5 3.0> > PS what's the right way to get integer labels?Do them by hand, if they are (numeric) integers> plot(x,y, axes = FALSE) > axis(2) > axis(1, at = x) > box()You could try writing your own Axis.integer function if doing the extra steps is a pain - something like: Axis.integer <- function(x = NULL, at = NULL, ..., side, labels = NULL) { at <- unique(x) labels <- as.character(at) axis(side = side, at = unique(x), labels = labels, ...) } which works in these case, y2 <- runif(3) plot(x, y) plot(x, y2) But is far from bullet proof and is not guaranteed to work in all situations. HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% 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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Mark Difford
2008-Feb-19 21:08 UTC
[R] Why does plot() ignore the data type for axis labels?
Hi Stiffler,>> I was wondering why the plot() command ignores the datatype when >> displaying axis labels...plot() doesn't ignore the datatype:> x <- as.integer(c(1,2,3)) > y <-x > typeof(x)[1] "integer"> mode(x)[1] "numeric" plot(x,y) calls xy.coords(), which recasts x as: x = as.double(x), which is fine, since x is (also/primarily) numeric. ??? See ?double, sub: "Note on names". HTH, Mark. Stiffler wrote:> > Hello, > > I was wondering why the plot() command ignores the datatype when > displaying axis labels. More specifically, if the data points are > integers then the axis labels should intuitively also be integers, right? > >> x <- as.integer(c(1,2,3)) >> y <-x >> typeof(x) > [1] "integer" >> plot(x,y) >> > > The axis labels are 1.0, 1.5, 2.0, 2.5, 3.0 but if the integer type were > taken into account they would be 1, 2, 3. > > PS what's the right way to get integer labels? > Z. >-- View this message in context: http://www.nabble.com/Why-does-plot%28%29-ignore-the-data-type-for-axis-labels--tp15562325p15567499.html Sent from the R help mailing list archive at Nabble.com.