如何从ggplot2注释中左对齐文本 [英] How to left align text in annotate from ggplot2

查看:269
本文介绍了如何从ggplot2注释中左对齐文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的例子是:

qplot(mtcars$mpg) + 
  annotate(geom = "text", x = 30, y = 3, label = "Some text\nSome more text")

如何使此处的文本对齐?这样某些"彼此对齐.

How do I get the text here to be left aligned? So that the 'Some's line up with each other.

推荐答案

hjust = 0可以满足您的要求. just 代表水平对齐,0左对齐,0.5居中,1右对齐.

hjust = 0 does what you want. hjust stands for horizontal justification, 0 will be left-justified, 0.5 will be centered, and 1 will be right-justified.

qplot(mtcars$mpg) +
    annotate(geom = "text", x = 30, y = 3,
             label = "Some text\nSome more text",
             hjust = 0)

有关垂直对齐方式,另请参见vjust.

See also vjust for vertical justification.

ggplot2中,只要设置了文本首选项,这些参数就会出现.调整主题选项时,它们适用于annotategeom_textelement_text.

In ggplot2, these arguments are present any time text preferences are set. They work for annotate, geom_text, or in element_text when adjusting theme options.

如果您查看?geom_text,则可以找到文本字符串选项:"left""middle""right"(对于hjust),"top""center""bottom" vjust,并且对于"inward""outward",它们始终会朝着中心或从中心向内调整.

If you look at ?geom_text, you can find text string options: "left", "middle", or "right", (for hjust), "top", "center", "bottom" for vjust, and for either "inward" and "outward" which will always adjust in toward or out away from the center.

在许多base图形功能中,此行为类似,例如paradj自变量,由text()mtext()title()使用,它们可以是长度为2的向量水平和垂直对齐.此外,axis()hadjpadj自变量用于对轴垂直 h 和与轴垂直的 p 对齐.

This behavior is similar in many base graphics functions, such as the adj argument for par, used by text(), mtext(), and title(), which can be vector of length 2 for the horizontal and vertical justificatons. Also the hadj and padj arguments to axis() for justifications horizontal to and perpendicular to the axis.

这篇关于如何从ggplot2注释中左对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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