132

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.

1 Answer 1

214

hjust = 0 or hjust = "left" 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. You can also use hjust = "inward" or hjust = "outward" to align text in toward the center or out away from the center.

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.

Differences with GUI editors

There's a bit of a difference in how text justification works in ggplot compared to how you may be used to in in a GUI document or slides editor. In those GUIs, you draw a textbox, and then you justify the text within that box--left justified goes to the left edge of the box, right justified goes to the right edge, and center in the middle.

In ggplot, there is no text box. You specify a single point for the text, and the justification touches that. You may expect that if you change from left-justified to right-justified, the text will move to the right, but it will actually move to the left, as it is the right margin that is moving. This code illustrates:

hjust_data = data.frame(z = paste("hjust =", c(0, 0.5, 1)))
df = data.frame(x = 1:5, y = 1:5)
df = merge(df, hjust_data, all = TRUE)

text_df = subset(df, x == 3) |>
  transform(lab = "Your nice\nmultiple line text\nis here")

ggplot(df, aes(x, y)) +
  geom_point() +
  facet_wrap(vars(z), ncol = 1) +
  geom_text(aes(label = lab), data = text_df[1, ], hjust = 0) +
  geom_text(aes(label = lab), data = text_df[2, ], hjust = 0.5) +
  geom_text(aes(label = lab), data = text_df[3, ], hjust = 1)

enter image description here

Comparison to base graphics

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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.
@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..."
A good answer can be found here : stackoverflow.com/a/7267364/5371553
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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.