Change the spacing of tick marks on the axis of a plot?
RPlotR Problem Overview
How can I change the spacing of tick marks on the axis of a plot?
What parameters should I use with base plot or with rgl
?
R Solutions
Solution 1 - R
There are at least two ways for achieving this in base graph (my examples are for the x-axis, but work the same for the y-axis):
-
Use
par(xaxp = c(x1, x2, n))
orplot(..., xaxp = c(x1, x2, n))
to define the position (x1
&x2
) of the extreme tick marks and the number of intervals between the tick marks (n
). Accordingly,n+1
is the number of tick marks drawn. (This works only if you use no logarithmic scale, for the behavior with logarithmic scales see?par
.) -
You can suppress the drawing of the axis altogether and add the tick marks later with
axis()
.
To suppress the drawing of the axis useplot(... , xaxt = "n")
.
Then callaxis()
withside
,at
, andlabels
:axis(side = 1, at = v1, labels = v2)
. Withside
referring to the side of the axis (1 = x-axis, 2 = y-axis),v1
being a vector containing the position of the ticks (e.g.,c(1, 3, 5)
if your axis ranges from 0 to 6 and you want three marks), andv2
a vector containing the labels for the specified tick marks (must be of same length asv1
, e.g.,c("group a", "group b", "group c")
). See?axis
and my updated answer to a post on stats.stackexchange for an example of this method.
Solution 2 - R
With base graphics, the easiest way is to stop the plotting functions from drawing axes and then draw them yourself.
plot(1:10, 1:10, axes = FALSE)
axis(side = 1, at = c(1,5,10))
axis(side = 2, at = c(1,3,7,10))
box()
Solution 3 - R
I have a data set with Time as the x-axis, and Intensity as y-axis. I'd need to first delete all the default axes except the axes' labels with:
plot(Time,Intensity,axes=F)
Then I rebuild the plot's elements with:
box() # create a wrap around the points plotted
axis(labels=NA,side=1,tck=-0.015,at=c(seq(from=0,to=1000,by=100))) # labels = NA prevents the creation of the numbers and tick marks, tck is how long the tick mark is.
axis(labels=NA,side=2,tck=-0.015)
axis(lwd=0,side=1,line=-0.4,at=c(seq(from=0,to=1000,by=100))) # lwd option sets the tick mark to 0 length because tck already takes care of the mark
axis(lwd=0,line=-0.4,side=2,las=1) # las changes the direction of the number labels to horizontal instead of vertical.
So, at = c(...)
specifies the collection of positions to put the tick marks. Here I'd like to put the marks at 0, 100, 200,..., 1000. seq(from =...,to =...,by =...)
gives me the choice of limits and the increments.
Solution 4 - R
And if you don't want R to add decimals or zeros, you can stop it from drawing the x axis or the y axis or both using ...axt. Then, you can add your own ticks and labels:
plot(x, y, xaxt="n")
plot(x, y, yaxt="n")
axis(1 or 2, at=c(1, 5, 10), labels=c("First", "Second", "Third"))
Solution 5 - R
I just discovered the Hmisc package:
> Contains many functions useful for data analysis, high-level graphics, utility operations, functions for computing sample size and power, importing and annotating datasets, imputing missing values, advanced table making, variable clustering, character string manipulation, conversion of R objects to LaTeX and html code, and recoding variables.
library(Hmisc)
plot(...)
minor.tick(nx=10, ny=10) # make minor tick marks (without labels) every 10th