119

My example is:

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.

kennyB
  • 1,963
  • 3
  • 17
  • 22

1 Answers1

195

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)

See also vjust for vertical justification.

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.

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.


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.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Great answer. @Gregor can you link us to a good article on ggplot's hidden parameters like these.. Most of the reference of ggplot says `... Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat.` ... which is not very helpful. – Lazarus Thurston Nov 09 '20 at 08:53
  • @LazarusThurston The parameters aren't hidden, they're documented thoroughly on the main geom pages. `annotate` lets you use most any geom, here `geom = "text"` was used, so the `?geom_text` help page has the relevant details. `?geom_text` has a heading for **Alignment**, which begins *"You can modify text alignment with the `vjust` and `hjust` aesthetics. These can either be a number between 0 (right/bottom) and 1 (top/left) or a character..."* – Gregor Thomas Nov 09 '20 at 14:06
  • 1
    A good answer can be found here : https://stackoverflow.com/a/7267364/5371553 – Ryderc Jan 05 '22 at 10:38
  • I agree with Lazarus - there's nothing in that man text that tells you to look at `geom_text` for text aesthetics related to `annotate`. It's obvious to those that already know! But to the newcomer it is not at all obvious that that is where you should look. The man would be better to mention (at least some of) the various geom_*() help pages by name. – Francis Barton Jan 24 '22 at 13:48
  • @FrancisBarton If you want the help page to change, file a documentation feature request at the main `ggplot2` github. – Gregor Thomas Jan 24 '22 at 14:46
  • @GregorThomas sure, I could do that. That's always an option in open source. But that doesn't invalidate Lazarus's observation (or my agreement with it) that *as things stand* we have experienced that it is not very clear for newcomers. One needs a certain amount of experience and confidence to file an issue. Your reply is completely true, yet doesn't really address our observations - which I think remain valid. They were not criticisms of you, so why the apparent defensiveness? – Francis Barton Jan 24 '22 at 18:05
  • 1
    @FrancisBarton I don't think I'm being defensive. You're commenting on an answer I posted 7 years ago, reviving a discussion from over a year ago with what looks to me to be a recommendation for a change the man page. The tone of my short sentence is admittedly hard to read--it's just a short sentence--but I guess I just don't really see the point of you adding a comment to this stale discussion, so I'm trying to direct you to a place you can make your point and have it be impactful. – Gregor Thomas Jan 24 '22 at 18:23
  • And the way you phrased your comment, *"The man would be better..."* seemed like a constructive and fairly specific suggestion for an improvement to the documentation--but not in a place where anyone who works on the documentation would ever see it. – Gregor Thomas Jan 24 '22 at 18:25
  • @GregorThomas that's fair, and yes I probably did read a tone into your comment that wasn't there. – Francis Barton Jan 25 '22 at 09:29