Hagen Brenner
Hagen Brenner

Reputation: 813

Setting of y axis in a plot

I have a very simple problem regarding plot settings.

I would like to have ticks (and labels on these ticks) on the y axis in a special way. For instance from 3 to 9, by one unit.

This is the code:

windows()         
par(yaxp = c(3, 9, 7))
plot(1:10)

But it does not work. And I really do not understand why? I also tried playing around with arguments from par() such as tck, tcl, yaxs, yaxt, yaxp and the function axis(). Which lead for example among others to the following codes:

windows()
par(yaxt = "n", yaxp = c(3, 9, 7))
plot(1:10)

or

windows()
par(yaxt = "n")
plot(1:10)
axis(2, at = c(3, 4, 5))

Unfortunately, I failed in every case... Any ideas??

Upvotes: 2

Views: 13922

Answers (3)

joran
joran

Reputation: 173707

You've received some good solutions to your immediate problem, but I thought I'd answer the "Why?" portion. R's documentation in this instance is correct, but perhaps not as transparent as it could be.

If you examine the section of ?par describing xaxp, you'll find:

This parameter is reset when a user coordinate system is set up, for example by starting a new page or by calling plot.window or setting par("usr"): n is taken from par("lab").

Under yaxp, the same warning is not given, but instead it says simple 'See xaxp'. There is no similar warning for the usr parameter in ?par, but the same is probably the case, since if we look up ?plot.window we see:

A side-effect of the call is to set up the usr, xaxp and yaxp

So what's happening is that a call to plot eventually leads to calling plot.window, a side effect of which is to actually reset the settings for usr, xaxp and yaxp. Unless, of course, you have passed those arguments directly to a higher level function like plot that in turn hands them off down the line to plot.window.

Personally, I think this deserves mention in the Details section of ?par along with the list of parameters that can only be set by using par() rather than passing them to high level plotting functions.

Upvotes: 7

Roman Luštrik
Roman Luštrik

Reputation: 70653

This works for me?

plot(1:10, yaxp = c(3, 9, 6))

enter image description here

Upvotes: 5

Tinu
Tinu

Reputation: 117

I normally solve this by setting axes = FALSE in the call to plot() and then use axis() to draw the individual axes.

# no call to par() needed
plot(c(1:10), axes = FALSE)
axis(1)                     # x-Axis
ticks <- seq(3, 9, 1)       # sequence for ticks and labels
axis(2, at = ticks,         # y-Axis
    labels = ticks)
box()                       # and a box around the plot

Upvotes: 6

Related Questions