Change the spacing of tick marks on the axis of a plot?
Asked Answered
R

5

83

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?

Rookie answered 24/9, 2010 at 7:43 Comment(1)
look at https://mcmap.net/q/190130/-minor-tick-position-in-r for the size of ticksActinomycin
A
102

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):

  1. 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.)

  2. 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.

Arlin answered 24/9, 2010 at 8:18 Comment(4)
Is there a way to control how many decimal places R uses for numerical tick labels. I just tried labels=c(0.0,0.5,1.0,1.5,2.0) but in the graph the labels were 0, 0.5, 1, 1.5, etc. Is there a way to get the same number of decimal places on all labels? I tried labels=round(seq(0,2,0.5),1) but the whole numbers still had no decimal places.Unread
try functions formatC or prettyNumfor creating character vectors to be passed to labels. E.g.: formatC(seq(0,2,0.5), digits = 1, format = "f")Arlin
+1 been looking for a while now to find this. Turns out though that 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
For me it doesn't work with par(xaxp..). It works only when I put it inside the plot(xaxp...)Muna
A
34

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()
Advowson answered 24/9, 2010 at 8:19 Comment(2)
Hello I have datetime data on the x axis, (chron) In my case I think it should be a better way than writing a line with let say 25 full dates. What If I want the dates writen vertically on the x axis, I mean perpendicularly. cheersRookie
Look at graphics parameter 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
D
7

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.

Decade answered 2/1, 2015 at 8:31 Comment(0)
I
3

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"))
Inez answered 15/7, 2015 at 9:0 Comment(1)
Of course, you can programmatically turn this on and off with a variable 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
K
2

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
Kesterson answered 29/8, 2018 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.