Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, how to set the breaks of x-axis?

Tags:

plot

r

Say here is my code:

plot(data ylab="x", xlab="y", xlim=c(1, 13))

But the x-axis of the plot presented like this: enter image description here

As you see, the x-axis presented from 2 to 12 by 2. However, I want it shown from 1 to 13 by 1 step, how can I realize that?

like image 479
Ping Tang Avatar asked Nov 26 '25 22:11

Ping Tang


1 Answers

You've defined the limits for your axis; however, R is inserting the "default" values for it.

To alter them, you need to

  1. "Override" the creation of the axis with xaxt='n'
  2. Define a custom axis

So, let's get it done!

plot(data, ylab="x", xlab="y", xlim=c(1, 13), xaxt='n')
# Now, define a custom axis
axis(side = 1, at=1:13)

This will give you what you want.


From the documentation, axis():

Description

Adds an axis to the current plot, allowing the specification of the side, position, labels, and other options.

Usage

axis(side, at = NULL, labels = TRUE, tick = TRUE, line = NA, pos = NA, outer = FALSE, font = NA, lty = "solid", lwd = 1, lwd.ticks = lwd, col = NULL, col.ticks = NULL, hadj = NA, padj = NA, ...)

Some of the most used arguments are:

  • side (integer) 1=below, 2=left, 3=above and 4=right.
  • at (vector) position of the tick marks
  • labels (vector) labels for the tick marks. The vector must be the same size of at. If ommited, the values of at will be used. FALSE hides any labels

Useful reference:

  • The Quick-R tutorial | Axes and text
like image 197
Barranka Avatar answered Nov 29 '25 12:11

Barranka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!