R语言 hjust = 0, vjust = 1属性设置

本文详细介绍了ggplot2中hjust和vjust参数的作用,通过实例展示了如何使用这两个参数来控制文本的水平和垂直对齐方式。此外,还探讨了在不同情境下调整这些参数的具体效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

这里写图片描述

&:色、线 和 点
这里写图片描述

Error in check.length("fill"): 'gpar' element 'fill' must not be length 0 Traceback: 1. heatmap_motor(mat, border_color = border_color, cellwidth = cellwidth, . cellheight = cellheight, treeheight_col = treeheight_col, . treeheight_row = treeheight_row, tree_col = tree_col, tree_row = tree_row, . filename = filename, width = width, height = height, breaks = breaks, . color = color, legend = legend, annotation_row = annotation_row, . annotation_col = annotation_col, annotation_colors = annotation_colors, . annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, . annotation_names_col = annotation_names_col, main = main, . fontsize = fontsize, fontsize_row = fontsize_row, fontsize_col = fontsize_col, . hjust_col = hjust_col, vjust_col = vjust_col, angle_col = angle_col, . fmat = fmat, fontsize_number = fontsize_number, number_color = number_color, . gaps_row = gaps_row, gaps_col = gaps_col, labels_row = labels_row, . labels_col = labels_col, ...) 2. heatmap_motor(matrix, cellwidth = cellwidth, cellheight = cellheight, . border_color = border_color, tree_col = tree_col, tree_row = tree_row, . treeheight_col = treeheight_col, treeheight_row = treeheight_row, . breaks = breaks, color = color, legend = legend, annotation_col = annotation_col, . annotation_row = annotation_row, annotation_colors = annotation_colors, . annotation_legend = annotation_legend, annotation_names_row = annotation_names_row, . annotation_names_col = annotation_names_col, filename = NA, . main = main, fontsize = fontsize, fontsize_row = fontsize_row, . fontsize_col = fontsize_col, hjust_col = hjust_col, vjust_col = vjust_col, . angle_col = angle_col, fmat = fmat, fontsize_number = fontsize_number, . number_color = number_color, labels_row = labels_row, labels_col = labels_col, . gaps_col = gaps_col, gaps_row = gaps_row, ...) 3. draw_annotation_legend(annotation, annotation_colors, border_color, . fontsize = fontsize, ...) 4. rectGrob(x = unit(0, "npc"), y = yy, hjust = 0, vjust = 1, height = 2 * . text_height, width = 2 * text_height, gp = gpar(col = border_color, . fill = annotation_colors[[i]])) 5. grob(x = x, y = y, width = width, height = height, just = just, . hjust = hjust, vjust = vjust, name = name, gp = gp, vp = vp, . cl = "rect") 6. gpar(col = border_color, fill = annotation_colors[[i]]) 7. validGP(list(...)) 8. check.length("fill") 9. stop(gettextf("'gpar' element '%s' must not be length 0", gparname), . domain = NA) 10. .handleSimpleError(function (cnd) . { . watcher$capture_plot_and_output() . cnd <- sanitize_call(cnd) . watcher$push(cnd) . switch(on_error, continue = invokeRestart("eval_continue"), . stop = invokeRestart("eval_stop"), error = NULL) . }, "'gpar' element 'fill' must not be length 0", base::quote(check.length("fill")))
最新发布
06-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值