R plot: How to use mtext to get top-aligned vertical label with las=1

前端 未结 2 1786
渐次进展
渐次进展 2021-01-14 01:05

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

相关标签:
2条回答
  • 2021-01-14 01:40

    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)
    

    0 讨论(0)
  • 2021-01-14 01:41

    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.

    enter image description here

    0 讨论(0)
提交回复
热议问题