简体   繁体   中英

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

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: 在此处输入图片说明

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?

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:

Have a look at this code: http://cran.r-project.org/doc/contrib/Lemon-kickstart/axbreak.R

You have to source that file in order to use the function:

source('http://cran.r-project.org/doc/contrib/Lemon-kickstart/axbreak.R')
axis.break(axis=1,breakpos,bgcol="white",breakcol="black", style=c("slash","zigzag"),brw=0.02)

Adapt to your liking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM