使用 ggplot 绘图时 hjust 和 vjust 做什么?
每次我使用 ggplot 绘制绘图时,我都会花一点时间在一行中尝试 hjust 和 vjust 的不同值,例如
+ opts(axis.text.x = theme_text(hjust = 0.5))
让轴标签在轴标签几乎接触轴的位置对齐,并与轴齐平(有理由可以这么说,轴)。但是,我真的不明白发生了什么事。通常,hjust = 0.5
会给出与 hjust = 0.6
截然不同的结果,例如,我无法仅通过使用不同的值来弄清楚它。
谁能向我指出 hjust 和 vjust 选项如何工作的全面解释?
Every time I make a plot using ggplot, I spend a little while trying different values for hjust and vjust in a line like
+ opts(axis.text.x = theme_text(hjust = 0.5))
to get the axis labels to line up where the axis labels almost touch the axis, and are flush against it (justified to the axis, so to speak). However, I don't really understand what's going on. Often, hjust = 0.5
gives such dramatically different results from hjust = 0.6
, for example, that I haven't been able to figure it out just by playing around with different values.
Can anyone point me to a comprehensive explanation of how hjust and vjust options work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
hjust
和vjust
的值仅定义在 0 和 1 之间:来源:ggplot2,Hadley Wickham,第 196 页
(是的,我知道在大多数情况下,您可以在超出此范围的情况下使用它,但不要期望它以任何特定方式运行。这超出了规范。)
hjust
控制水平方向理由和vjust
控制垂直对齐。一个示例应该可以清楚地说明这一点:
要了解更改
hjust
在轴文本中,您需要了解轴文本的水平对齐不是相对于 x 轴定义的,而是相对于整个绘图(其中包括 y 轴文本)定义的。 (在我看来,这是不幸的。相对于轴进行对齐会更有用。)要探索轴标签的
vjust
对齐会发生什么:The value of
hjust
andvjust
are only defined between 0 and 1:Source: ggplot2, Hadley Wickham, page 196
(Yes, I know that in most cases you can use it beyond this range, but don't expect it to behave in any specific way. This is outside spec.)
hjust
controls horizontal justification andvjust
controls vertical justification.An example should make this clear:
To understand what happens when you change the
hjust
in axis text, you need to understand that the horizontal alignment for axis text is defined in relation not to the x-axis, but to the entire plot (where this includes the y-axis text). (This is, in my view, unfortunate. It would be much more useful to have the alignment relative to the axis.)To explore what happens with
vjust
aligment of axis labels:最明确的可能是 ggplot2 书第一版的图 B.1(d)。
注:该书的第三版可在 https://ggplot2-book.org/ 似乎没有这些附录或这个数字。
然而,事情并不那么简单。
hjust
和vjust
正如所描述的那样,它们在geom_text
和theme_text
中是如何工作的(有时)。一种思考方法是在文本周围想象一个框,参考点与该框的关系,单位相对于框的大小(因此对于不同大小的文本来说是不同的)。 0.5 的hjust
和 0.5 的vjust
使框位于参考点的中心。减少hjust
会将框向右移动框宽度乘以0.5-hjust
的量。因此,当hjust=0
时,框的左边缘位于参考点。增加hjust
将框向左移动框宽度乘以hjust-0.5
的量。当hjust=1
时,框从中心向左移动半框宽度,这将右边缘放在参考点上。如果hjust=2
,则框的右边缘是参考点左侧的框宽度(中心是参考点左侧的2-0.5=1.5
框宽度。对于垂直方向,这实际上是图 B.1(d) 所说的,但它的推断超出了 [0,1],例如
后三个图。我不知道为什么会这样。另外,如果文本旋转,则情况会更复杂,
第一个将标签左对齐(相对于底部),第二个将它们集中在某个框中,以便它们的中心对齐,第三个将它们右对齐。 (所以它们的右侧在轴旁边对齐),好吧,我无法以连贯的方式解释它与文本的大小、最宽文本的大小有关。不知道还有什么。
Probably the most definitive is Figure B.1(d) of the first edition of the ggplot2 book.
Note: the third edition of the book available at https://ggplot2-book.org/ does not appear to have these appendices nor this figure.
However, it is not quite that simple.
hjust
andvjust
as described there are how it works ingeom_text
andtheme_text
(sometimes). One way to think of it is to think of a box around the text, and where the reference point is in relation to that box, in units relative to the size of the box (and thus different for texts of different size). Anhjust
of 0.5 and avjust
of 0.5 center the box on the reference point. Reducinghjust
moves the box right by an amount of the box width times0.5-hjust
. Thus whenhjust=0
, the left edge of the box is at the reference point. Increasinghjust
moves the box left by an amount of the box width timeshjust-0.5
. Whenhjust=1
, the box is moved half a box width left from centered, which puts the right edge on the reference point. Ifhjust=2
, the right edge of the box is a box width left of the reference point (center is2-0.5=1.5
box widths left of the reference point. For vertical, less is up and more is down. This is effectively what that Figure B.1(d) says, but it extrapolates beyond [0,1].But, sometimes this doesn't work. For example
The three latter plots are identical. I don't know why that is. Also, if text is rotated, then it is more complicated. Consider
The first has the labels left justified (against the bottom), the second has them centered in some box so their centers line up, and the third has them right justified (so their right sides line up next to the axis). The last one, well, I can't explain in a coherent way. It has something to do with the size of the text, the size of the widest text, and I'm not sure what else.