I\'m trying to get a label on my vertical axis using mtext
that is read horizontally (las=1
) and is at the top of the axis.
My attempt is t
The trick is to access the height of the plot by calling par('usr')[4]
:
par(mar=c(2,3,2,1))
plot(1, 1, ann=F)
mtext(col="blue", "y", side=2, line=2, at=par('usr')[4], las=2)
Here's a possibility using text()
. Basically we are setting the x and y position but we are doing so in a way that will adapt to the plot as you change ranges and such. Thus you shouldn't have to change the code too much for different plot (only if you change the margin spacing)
par(mar=c(2,3,2,1))
plot(1, 1, ann=F)
mtext(side=3, "convertX/Y")
xx<-grconvertX(grconvertX(0, "nfc","inches") , "inches","user")
yy<-grconvertY(par("din")[2]-par("cin")[2]*5/2, "inches","user")
text(xx,yy,"y", col="green", cex=1.5, xpd=NA, pos=4)
For the x, we go all the way to the left of the figure region, and for the y, we go down just over two lines form the top. Then we draw our "y" value to that it is left aligned at that point.