R plot:如何使用 mtext 获取 las=1 的顶部对齐的垂直标签 [英] R plot: How to use mtext to get top-aligned vertical label with las=1

查看:25
本文介绍了R plot:如何使用 mtext 获取 las=1 的顶部对齐的垂直标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 mtext 在我的垂直轴上获取一个标签,该标签是水平读取的 (las=1),并且位于轴的顶部.

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.

我的尝试是使用 las=1, adj=1.当我不指定 las=1 时,我可以获得所需的位置,但是一旦我添加了 las=1 参数 adj=1 位置消失.这是带有代码的图片.左图显示了正确的位置,但没有 las=1.右图显示了存在的两个参数.

My attempt is to use las=1, adj=1. I can get the desired placement when I don't specify las=1, but as soon as I add the las=1 argument the adj=1 placement goes away. Here's a picture with code. The left plot shows correct placement, but without las=1. The right plot shows both arguments present.

par(mfrow=c(1,2), mar=c(2,3,2,1))

plot(1, 1, ann=F)
mtext(col="blue", "y", side=2, line=2, adj=1)
mtext(side=3, "col=blue, side=2, adj=1")

plot(1, 1, ann=F)
mtext(col="red", "y", side=2, line=2, adj=1, las=1)
mtext(side=3, "col=red, side=2, adj=1, las=1")

我试过用 padj 大惊小怪,但这只会将标签向上移动 &下来一点.另外,我知道 at 参数可以,但感觉有点太手动了.

I've tried fussing around with padj, but that only moves the label up & down a little. Also, I know that the at argument can, but that feels a bit too manual.

推荐答案

诀窍是通过调用par('usr')[4]来访问绘图的高度:

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)

这篇关于R plot:如何使用 mtext 获取 las=1 的顶部对齐的垂直标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆