如何在ggplot2的注释中保持对齐文本

ken*_*nyB 75 r annotate text-alignment ggplot2

我的例子是:

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

如何让这里的文字保持对齐?所以'有些人相互排队.

Gre*_*gor 115

hjust = 0做你想要的.hjust代表水平对齐,0表示左对齐,0.5表示居中,1表示右对齐.

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

另请参阅vjust垂直对齐.

ggplot2,任何时候设置文本首选项时都会出现这些参数.他们的工作进行annotate,geom_text或在element_text调整主题选项时.

如果你看一下?geom_text,你可以找到文本字符串选项:"left","middle",或"right",(对于hjust)"top","center","bottom"vjust,和任何"inward""outward"这将永远朝向或从中心向外远离调整英寸


这种现象是在许多类似的base图形功能,如adj对参数par,通过使用text(),mtext()title(),其可以是长度为2的向量的水平和垂直justificatons.另外,hadjpadj参数axis()为理由ħ orizo​​ntal到和p erpendicular于轴线.