BMS
BMS

Reputation: 225

Best way to control axis label position parallel to axis

The axis labels for plots in R default to the center of their respective axis. I would like to move the axis labels to the ends of the axes so that the horizontal "x" label is at the far right and the vertical "y" label is at the far top. What are some recommended ways to do this? Can one use mtext in a clever way?

Upvotes: 2

Views: 2573

Answers (2)

thelatemail
thelatemail

Reputation: 93938

Using the title function you can also use the adj argument, and the text will be placed the same distance from the axis as titles are by default:

plot(0,ann=FALSE)
title(xlab="right",ylab="top",adj=1)

In fact, you can do this all from within the plot call too:

plot(0,adj=1,xlab="right",ylab="top")

enter image description here

Be aware that this second example will also right align the main title in the instance of something like:

plot(0,adj=1,xlab="right",ylab="top",main="yeah")

Upvotes: 2

agstudy
agstudy

Reputation: 121618

Using mtext and playing with adj parameter:

plot(0,ann=FALSE)
mtext('right',side=1,line=2,adj=1,col='red',cex=2)
mtext('top',side=2,line=2,adj=1,col='blue',cex=2)

enter image description here

Upvotes: 3

Related Questions