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

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

问题描述

每次我使用ggplot进行绘图时,我会花费一些时间,尝试使用不同的hjust和vjust值,如

  + opts(axis.text.x = theme_text(hjust = 0.5))

轴标签排列在轴标签几乎与轴接触的位置,并与其齐平(可以说与轴对齐)。但是,我不太明白发生了什么事。通常情况下, hjust = 0.5 会给出如此明显不同于 hjust = 0.6 的结果,例如,我没有能够通过玩弄不同的价值来弄清楚。

任何人都可以指出我对hjust和vjust选项是如何工作的全面解释吗?

解决方案

hjust vjust 的值仅在0和1之间定义:




  • 0表示左对齐

  • <1>表示右对齐


资料来源:ggplot2,Hadley Wickham,第196页
$ b 大多数情况下,您可以在此范围之外使用它,但不要指望它以任何特定方式运行)。



hjust 控制水平对齐和 vjust 控制垂直对齐。



清楚:

  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轴文本)进行定义的。 (在我看来,这是不幸的,相对于坐标轴的对齐会更有用。)

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


p1 < - p + opts( axis.title.x = theme_text(hjust = 0))+ xlab(x轴在hjust = 0)
p2 < - p + opts(axis.title.x = theme_text(hjust = 0.5) )+ xlab(hjust = 0.5时的X轴)
p3 < - p + opts(axis.title.x = theme_text(hjust = 1))+ xlab(hjust = 1时的X轴)

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



< hr>

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

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

p1 < - p + opts(axis.text.x = theme_text(vjust = 0,color =red))+
xlab(X轴标签(vjust = 0,color =red))+
xlab(X轴标签)对齐vjust = 0)
p2 < - p + opts(axis.text.x = theme_text (vjust = 0.5))
p3 < - p + opts(axis.text.x = theme_text(vjust = 1,color =red))+
xlab(X轴标签与vjust = 1对齐)


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


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?

解决方案

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

  • 0 means left-justified
  • 1 means right-justified

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


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") +
    opts(axis.title.y=theme_text(angle=0))


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

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


To explore what happens with vjust aligment of axis labels:

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

p1 <- p + opts(axis.text.x=theme_text(vjust=0, colour="red")) + 
        xlab("X-axis labels aligned with vjust=0")
p2 <- p + opts(axis.text.x=theme_text(vjust=0.5, colour="red")) + 
        xlab("X-axis labels aligned with vjust=0.5")
p3 <- p + opts(axis.text.x=theme_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天全站免登陆