The value of hjust and vjust are only defined between 0 and 1:
(hjust 和 vjust的值限定在0-1)
0 means left-justified(0表示左适应)
1 means right-justified(1表示右适应)
Source: ggplot2, Hadley Wickham, page 196
(资料来源于ggplot2, Hadley Wickham, page 196)
hjust controls horizontal justification and vjust controls vertical justification.
(hjust 控制horizontal横轴 , vjust控制vertical纵轴 )
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.)
(想要理解当你改变hjust时对图像的影响,你需要明白在axis text中,horizontal alignment是针对整个plot而不是x-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:
(来看看改变vjust产生的影响)
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)
&:色、线 和 点