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
?
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
?
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))
or plot(..., 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 use plot(... , xaxt = "n")
.
Then call axis()
with side
, at
, and labels
: axis(side = 1, at = v1, labels = v2)
. With side
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), and v2
a vector containing the labels for the specified tick marks (must be of same length as v1
, 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.
formatC
or prettyNum
for creating character vectors to be passed to labels
. E.g.: formatC(seq(0,2,0.5), digits = 1, format = "f")
–
Arlin n
is actually number of tick marks -1
. I wanted tick marks from 1
to 9
and had to set xaxp = c(1, 9, 8)
or I would not get the desired result. –
Spiritism par(xaxp..)
. It works only when I put it inside the plot(xaxp...)
–
Muna 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()
las
in ?par which can be added to a call to axis()
, and which controls the orientation of the tick labels. E.g. axis(side = 1, at = c(1,5,10), las = 2)
. You might want to investigate whether there is an axis()
function for chron objects or consider using R's Date Time classes, which already have axis()
functions (e.g. axis.Date()
for the class '"Date"'). –
Advowson 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.
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"))
flag
with yaxt=flag
BUT the trick is if you want to standard labels on, then set flag <- "s"
and if you don't want the standard labels then use flag <- "n"
(Adding this, because it took me a while to find "s") –
Kesterson 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
© 2022 - 2024 — McMap. All rights reserved.