使用 ggplot 绘制绘图时,hjust 和 vjust 会做什么? [英] What do hjust and vjust do when making a plot using ggplot?

查看:41
本文介绍了使用 ggplot 绘制绘图时,hjust 和 vjust 会做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我使用 ggplot 绘制一个图时,我都会花一些时间在像

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))

使轴标签在轴标签几乎接触轴的位置对齐,并与轴齐平(可以说是与轴对齐).但是,我真的不明白发生了什么.通常,hjust = 0.5 给出与 hjust = 0.6 截然不同的结果,例如,我无法通过尝试不同的值来弄清楚.

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.

谁能给我一个关于 hjust 和 vjust 选项如何工作的全面解释?

Can anyone point me to a comprehensive explanation of how hjust and vjust options work?

推荐答案

hjustvjust 的值只定义在 0 和 1 之间:

The value of hjust and vjust are only defined between 0 and 1:

  • 0 表示左对齐
  • 1 表示右对齐

来源:ggplot2,Hadley Wickham,第 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 控制水平对齐,vjust 控制垂直对齐.

hjust controls horizontal justification and vjust controls vertical justification.

一个例子应该清楚这一点:

An example should make this clear:

td <- expand.grid(
    hjust=c(0, 0.5, 1),
    vjust=c(0, 0.5, 1),
    angle=c(0, 45, 90),
    text="text"
)

ggplot(td, aes(x=hjust, y=vjust)) + 
    geom_point() +
    geom_text(aes(label=text, angle=angle, hjust=hjust, vjust=vjust)) + 
    facet_grid(~angle) +
    scale_x_continuous(breaks=c(0, 0.5, 1), expand=c(0, 0.2)) +
    scale_y_continuous(breaks=c(0, 0.5, 1), expand=c(0, 0.2))

要了解更改轴文本中的 hjust 时会发生什么,您需要了解轴文本的水平对齐方式不是与 x 轴相关,而是与整个绘图相关(其中包括 y 轴文本).(在我看来,这是不幸的.相对于轴进行对齐会更有用.)

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.)

DF <- data.frame(x=LETTERS[1:3],y=1:3)
p <- ggplot(DF, aes(x,y)) + geom_point() + 
    ylab("Very long label for y") +
    theme(axis.title.y=element_text(angle=0))


p1 <- p + theme(axis.title.x=element_text(hjust=0)) + xlab("X-axis at hjust=0")
p2 <- p + theme(axis.title.x=element_text(hjust=0.5)) + xlab("X-axis at hjust=0.5")
p3 <- p + theme(axis.title.x=element_text(hjust=1)) + xlab("X-axis at hjust=1")

library(ggExtra)
align.plots(p1, p2, p3)

探索 vjust 轴标签对齐会发生什么:

To explore what happens with vjust aligment of axis labels:

DF <- data.frame(x=c("a
a","b","cdefghijk","l"),y=1:4)
p <- ggplot(DF, aes(x,y)) + geom_point()

p1 <- p + theme(axis.text.x=element_text(vjust=0, colour="red")) + 
        xlab("X-axis labels aligned with vjust=0")
p2 <- p + theme(axis.text.x=element_text(vjust=0.5, colour="red")) + 
        xlab("X-axis labels aligned with vjust=0.5")
p3 <- p + theme(axis.text.x=element_text(vjust=1, colour="red")) + 
        xlab("X-axis labels aligned with vjust=1")


library(ggExtra)
align.plots(p1, p2, p3)

这篇关于使用 ggplot 绘制绘图时,hjust 和 vjust 会做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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