Helmut
Helmut

Reputation: 383

R axis not displayed

I am afraid I have lost the plot, literally! Why does the axis command not put an x axis in the following plot? It must be something ridiculous, as I can't simplify much more.

yar <- c(.2,.1,.05,.03,.02)
plot(yar,xaxt='n')
axis(1, at=c(0.01,0.02,0.03,0.04,0.05))

Upvotes: 6

Views: 15584

Answers (2)

SabDeM
SabDeM

Reputation: 7190

Because the plot function need two elements (coordinates): x and y. You provided the y coordinates and without user defined x coordinates, R assigns standard 1:n coordinates where n is equal to the number of points, in this case 5.

With your data try this:

yar <- c(.2,.1,.05,.03,.02)
plot(yar, xaxt='n')
axis(1, at=c(1, 2, 3, 4, 5))

It has this this output:

enter image description here

As a solution here is one approach: you can place the x coordinates at the default values selected by R and then you can uses labels as you wish. Look at the following code and especially the labels argument of the axis function.

yar <- c(.2,.1,.05,.03,.02)
plot(yar, xaxt='n')
axis(1, at = c(1, 2, 3, 4, 5), labels = as.character(sort(yar)))

which produces:

enter image description here

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

The reason your x axis is not appearing is that your placed it in a region of the plot where it is so small that it is not visible as output. You issued the following plot command:

plot(yar, xaxt='n')

which is really the same as doing

plot(c(1:5), yar, xaxt='n')

Since you never specified any x values, the default x value are just the counting numbers 1 through 5 corresponding to y values you did specify.

The solution to the problem is to place the x-axis where it will be visible. Hence you can try the following code:

xar <- 0.01*c(1:5)
yar <- c(.2,.1,.05,.03,.02)
plot(xar, yar, xaxt='n')
axis(1, at=xar)

Upvotes: 2

Related Questions