Skip to content

axes labeling

5 messages · David L Carlson, Sam Steingold, William Dunlap +1 more

#
Is it possible to control formatting of the numbers which go along the
axes in plots?
e.g.
plot(x=1:1000000,y=1:1000000)
will label the X axis as "0d+00", "2e+05" &c.
I want that to read 0, 200k, 400k &c.
I know of the function axis(), but it offers far too much control for
this simple task.
thanks.
#
It is possible, but only by using axis() since you can specify axis breaks
in a plot command, but not the labels. You can ignore most of the axis()
options so the commands are pretty simple:

plot(x=c(1, 1000000), y=c(1, 1000000), xlab="x", ylab="y", 
     xaxt="n", yaxt="n", las=2)
pos <- c(0, 200000, 400000, 600000, 800000, 1000000)
lbl <- c("0", "200k", "400k", "600k", "800k", "1000k")
axis(1, pos, lbl)
axis(2, pos, lbl)
# or axis(2, pos, lbl, las=2) to rotate the y tick mark labels.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
#
That's what I meant when I said "too much control".
I am happy with the way R selects positions.
All I want is a say in the way R formats those positions.

Think in terms of 1000000 being a variable.
To use axis, I will need to write a map from variable range to axis tick
positions first, and then sapply my formatting to the positions.
#
By "too much control" do you mean that axis requires too many inputs?
You can use axTicks to get the positions of the tick marks that would have
been drawn and create labels based on those positions.  E.g.,
at <- axTicks(side = side)
    lab <- ifelse(abs(at)>=1e6, paste(at/1e6, "M"), paste0(at/1e3, "k")) # alter to suit your tastes
    axis(side = side, at = at, lab = lab)
}
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
On 12/21/2012 07:12 AM, Sam Steingold wrote:
Hi Sam,
You can use the "pretty" function to get the default positions of the 
axis labels by passing the range of the values:

pretty(1:1000000)
[1] 0e+00 2e+05 4e+05 6e+05 8e+05 1e+06

Then you can pass these values to the axis function with your labels 
(0k, 200k, ...)

Jim